属性集上的Python断言

属性集上的Python断言,python,Python,是否可以在更改属性时进行属性断言(用于调试) 编辑: 这就是你要找的吗 class MyClass(object): def __init__(self): self.trigger = False self._my_property = 0 def set_my_property(self, value): if self.trigger: raise Exception("WHOOPS!")

是否可以在更改属性时进行属性断言(用于调试)

编辑: 这就是你要找的吗

class MyClass(object):
    def __init__(self):
        self.trigger = False
        self._my_property = 0

    def set_my_property(self, value):
        if self.trigger:
            raise Exception("WHOOPS!")
        self._my_property = value
        # TODO: mark my_property so that if it gets set again, an assert
        # is triggered
        self.trigger = True

    def get_my_property(self):
        return self._my_property

    my_property = property(get_my_property, set_my_property, None)

c = MyClass()
c.set_my_property(4)

# any of these lines would cause an assertion
c.set_my_property(8)
c.my_property = 123
编辑: 这就是你要找的吗

class MyClass(object):
    def __init__(self):
        self.trigger = False
        self._my_property = 0

    def set_my_property(self, value):
        if self.trigger:
            raise Exception("WHOOPS!")
        self._my_property = value
        # TODO: mark my_property so that if it gets set again, an assert
        # is triggered
        self.trigger = True

    def get_my_property(self):
        return self._my_property

    my_property = property(get_my_property, set_my_property, None)

c = MyClass()
c.set_my_property(4)

# any of these lines would cause an assertion
c.set_my_property(8)
c.my_property = 123

添加一个布尔值以检查该值是否已在之前设置:

编辑:但您需要一个属性,因此需要创建一个:

class MyClass(object):
    def __init__(self):
        self.my_property_set = False
        self._my_property = None

    def set_my_property(self, value):
        self._my_property = value
        assert not self.my_property_set,"my_property already set"
        self.my_property_set = True

    def get_my_property(self):
        return self._my_property

    my_property = property(get_my_property, set_my_property, None)

c = MyClass()
c.set_my_property(4)

# any of these lines would cause an assertion
c.set_my_property(8)
c.my_property = 123

添加一个布尔值以检查该值是否已在之前设置:

编辑:但您需要一个属性,因此需要创建一个:

class MyClass(object):
    def __init__(self):
        self.my_property_set = False
        self._my_property = None

    def set_my_property(self, value):
        self._my_property = value
        assert not self.my_property_set,"my_property already set"
        self.my_property_set = True

    def get_my_property(self):
        return self._my_property

    my_property = property(get_my_property, set_my_property, None)

c = MyClass()
c.set_my_property(4)

# any of these lines would cause an assertion
c.set_my_property(8)
c.my_property = 123

你能举例说明你的意思吗?你能举例说明你的意思吗?
c.my_property=123
不会这样断言。我的错,我忘了使用property()内置属性如果你想以
c.my_property=123
的身份访问它,你需要将它设置为
属性,这样会引发异常,而不是断言:-)呃,你可以通过…把它变成一个断言,包括一个断言。这是概念的证明,好吗?:)
c.my_property=123
不会以这种方式断言。糟糕,我忘了使用property()内置属性。如果要以引发异常而不是断言的
c.my_property=123
的身份访问它,则需要将其设置为
属性。这是概念的证明,好吗?:)
\u-bar
不应该是成员变量而不是类变量吗?
\u-bar
不应该是成员变量而不是类变量吗?