python的实例非常灵活,在某些情况下,如果想实现隐藏和封装性时就需要一些小操作来提高python的准确性.
_属性只是提供模块级别的隐藏,定义_属性,在实例中,仍然可以随意操作。
三个办法变得靠谱
# 1.藏. 私有属性 __name , 达到隐藏的效果,但并不能保证属性被意外染指
# 2.护 property函数 ,达到封装性,只有显示操作才能达到效果
# 3.限 __slot__ : 给python实例这个野孩子加紧箍咒, 禁止随意添加属性
# -*-coding:utf-8 -*-
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):
# File "C:/Workspace/test.py", line 42, in <module>
# a.dajiangyou = 456
# AttributeError: 'A' object has no attribute 'dajiangyou'
#这个__name是动态加的李鬼,有了slot,李鬼立刻现形了。
a.__name = 'yyy'
# Traceback (most recent call last):
# File "C:/Workspace/decorate/test.py", line 50, 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', 'setName']