python的实例非常灵活,在某些情况下,如果想实现隐藏和封装性时就需要一些小操作来提高python的准确性.
# 1.藏. 私有属性 __name , 达到隐藏的效果,但并不能保证属性被意外染指
# 2.护 property函数 ,达到封装性,只有显示操作才能达到效果
# 3.限 __slot__ : 给python实例这个野孩子加紧箍咒, 禁止随意添加属性
# -*-coding:utf-8 -*- """ @author: 回忆书签 @version: 0.1 @date: 2010-06-16 @description: python类中属性的私有封装 """ class A(object): def __init__(self,name): self.__name=name # 第一招 :私有属性 self.abc=123 @property def name(self): # 第二招, 公有方法name得到__name,在外界看name是一个属性,而实际上name是一个函数的返回值. # 而函数的输出是不能直接改变的,除非直接修改__name,而第一个办法已经把__name藏起来了。. return self.__name @name.setter def setName(self,value): #共用方法setName,这里没有命名为name,是故意让使用者意识到自己在做什么。 #想要改变__name只有通过这个入口 if not isinstance(value,str): raise TypeError('must be a string') self.__name=value @name.deleter def delName(self): del self.__name __slots__ = ('__name','abc') #第三招限制实例属性,将墙内的属性都规定好,以后再想挤进来属性就只能到这里来定义
if __name__ == "__main__": a=A('abc') print a.name a.setName='xyz' print a.name a.abc=4567
a.dajiangyou=456
Traceback (most recent call last): a.dajiangyou=456 AttributeError: 'A' object has no attribute 'dajiangyou'
a.__name='yyy'
Traceback (most recent call last): File "encloseattr.py", line 48, in <module> # a.__name='yyy' AttributeError: 'A' object has no attribute '__name'
print dir(a) # 墙内众生真面目: 谁是公有的谁是私有的一看便知道 # ['_A__name', # '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', # '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', # '__subclasshook__', # 'abc', #公有属性,没有任何修饰 # 'delName', # 'name', #公有的getter # 'setName' #公有的setter