简体   繁体   English

重温python,为什么这不抛出ValueError?

[英]Brushing up on python, why isn't this throwing a ValueError?

I'm brushing up on my python.我正在复习我的蟒蛇。 I've been coding in java for a year, but I'm about to take an algorithms class in Python.我已经用 Java 编码了一年,但我即将在 Python 中学习算法课程。 In preparation, I am duplicating the Lights Out puzzle game as a personal practice.在准备过程中,我正在复制 Lights Out 益智游戏作为个人练习。 I've written the first logic for the light object, and I've designed it to throw an error when I construct a Light() with a negative 0 value, but it won't throw an exception no matter what I construct the object with.我已经为 light 对象编写了第一个逻辑,并且我将它设计为在我构造具有负 0 值的 Light() 时抛出错误,但无论我构造什么对象都不会抛出异常和。

For example: light Light(1,1) is valid, but light Light(-1,-1) should throw a ValueError.例如: light Light(1,1)是有效的,但是light Light(-1,-1)应该抛出 ValueError。 Where am I going wrong?我哪里错了?

# Light.py represents a light object for LightsOut game.
# written by nrsmac

class Light:
    # Light.py represents a light object for LightsOut game.
    # written by nrsmac

    #Constants for Light States
    OFF = 0
    ON  = 1

    #Each light has a positive x and y position, negative 1 by defailt
    x = -1
    y = -1

    currentState = OFF

    #Constructor here
    def __init__(self, x, y):
        self.x = x
        self.y = y

        if self.isValidLocation() == False:
            raise ValueError('Invalid Location!')

    def toggle(self): # TODO: Make this change color
        if self.currentState == self.ON:
            self.currentState = self.OFF
        elif self.currentState == self.OFF:
            self.currentState = self.ON

    def printCurrentLightState(self):
        print(self.currentState)

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def setLightState(self, state):
        self.currentState = state

    def isValidLocation(self):
        if self.x < 0 | self.y < 0:
            return False

Does python always have this many self calls? python 总是有这么多的self调用吗? It's starting to get exhausting from a Java perspective.从 Java 的角度来看,它开始变得令人筋疲力尽。

I think that in your case the usage of |我认为在你的情况下使用| is not correct.是不正确的。 You have to use or , like so:你必须使用or ,像这样:

if (self.x < 0) or (self.y < 0):

Basic getters/setters like that aren't a thing in Python.像这样的基本 getter/setter 不是 Python 中的东西。

Having to type self over and over again is indeed annoying.不得不一遍又一遍地输入self确实很烦人。 However, we also have IDEs and text editors which support autocompletion, so it shouldn't be a massive issue.但是,我们也有支持自动完成的 IDE 和文本编辑器,所以这应该不是一个大问题。


This is what I would do to make the code more pythonic:这就是我要使代码更加 Pythonic 的方法:

@dataclass(init=False)
class Light:
    x: int
    y: int
    is_on: bool

    def __init__(self, x: int, y: int, is_on: bool = False) -> None:
        if x < 0 or y < 0:
            raise ValueError(f'Invalid location arguments in Light constructor: {x}, {y}')
        else:
            self.x = x
            self.y = y
            self.is_on = is_on

    def toggle(self) -> None:
        self.is_on = not self.is_on 

It's a work in progress, I think it can still be improved.这是一项正在进行中的工作,我认为它仍然可以改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM