setter method of property decorator not being called

前端 未结 1 1310
暗喜
暗喜 2021-01-12 06:19

I am trying to use a property method to set the status of a class instance, with the following class definition:

class Result:
    def __init__(self,x=None,y         


        
相关标签:
1条回答
  • 2021-01-12 07:10

    On Python 2, you must inherit from object for properties to work:

    class Result(object):
    

    to make it a new-style class. With that change your code works:

    >>> res = Result(5,6)
    >>> res.visible
    False
    >>> res.visible = True
    >>> res.currentStatus()
    'You can see me!'
    
    0 讨论(0)
提交回复
热议问题