What's the pythonic way to use getters and setters?

后端 未结 8 2316
盖世英雄少女心
盖世英雄少女心 2020-11-21 23:05

I\'m doing it like:

def set_property(property,value):  
def get_property(property):  

or

object.property = value  
value =         


        
8条回答
  •  -上瘾入骨i
    2020-11-22 00:03

    In [1]: class test(object):
        def __init__(self):
            self.pants = 'pants'
        @property
        def p(self):
            return self.pants
        @p.setter
        def p(self, value):
            self.pants = value * 2
       ....: 
    In [2]: t = test()
    In [3]: t.p
    Out[3]: 'pants'
    In [4]: t.p = 10
    In [5]: t.p
    Out[5]: 20
    

提交回复
热议问题