Python类中私有属性
''' 私有属性 说明: 1.私有属性不能在外面通过实例调用 2.只能够在类里面自己来调用 3.私有属性对外面是隐藏的 ''' class Animal(object): hobbie = "meat" def __init__(self,name): self.name = name self.__num = None @property def total_players(self): print(self.__num) @total_players.setter def total_players(self,num): self.__num = num print("total players:",self.__num) d = Animal("Eric") print(d.total_players) #None d.total_players = 4 print(d.total_players) #total players: 4 d.__num = 9 #这样赋值(不会给类里面的__num私有变量赋值),只是在d对象中重新创建了一个__num属性,值等于9 print(d.__num) #9 #其实用(_类名__属性名)还是能调用私有属性 print("OUT:",d._Animal__num) #OUT: 4