python中Property替代setter和getter方法处理私有化属性

class Person(object):
    def __init__(self):
        self.__money = 1

    @property
    def money(self):
        return self.__money

    @money.setter
    def money(self, num):
        self.__money = num


p = Person()
print(p.money)
p.money = 100
print(p.money)

你可能感兴趣的:(python中Property替代setter和getter方法处理私有化属性)