somepeople

python面向对象之propety

将动态函数变成静态“属性”,操作这个函数实际上操作这个对象的某个属性

class people:
    def __init__(self,name):
        self.__name = name
    #
    @property
    def name(self):
        return self.__name
    @name.setter
    def name(self,value):
        self.__name = value
    @name.deleter
    def name(self):
        print('不允许删除')
    #     # del self.__name
    #     del self.__name
p = people('ryan')
print(p.name)
p.name = 'tom'
print(p.name)
del p.name
print(p.name)
print(p.name)

#ryan
#tom
#不允许删除
#tom
#tom

 

posted on 2020-08-31 21:27  Hello_Thanos  阅读(393)  评论(0编辑  收藏  举报

导航