Python学习小记(4)---class
1.名稱修改機(jī)制
大概是會(huì)對形如 __parm 的成員修改為 _classname__spam
9.6. Private Variables
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g.
_spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form
__spam(at least two leading underscores, at most one trailing underscore) is textually replaced with_classname__spam, whereclassnameis the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls. For example:
class Mapping:
def __init__(self, iterable):
self.items_list = []
self.__update(iterable) def update(self, iterable):
for item in iterable:
self.items_list.append(item) __update = update # private copy of original update() method class MappingSubclass(Mapping): def update(self, keys, values):
# provides new signature for update()
# but does not break __init__()
for item in zip(keys, values):
self.items_list.append(item)The above example would work even if
MappingSubclasswere to introduce a__updateidentifier since it is replaced with_Mapping__updatein theMappingclass and_MappingSubclass__updatein theMappingSubclassclass respectively.Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
Notice that code passed to
exec()oreval()does not consider the classname of the invoking class to be the current class; this is similar to the effect of theglobalstatement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies togetattr(),setattr()anddelattr(), as well as when referencing__dict__directly.
試驗(yàn)結(jié)果如下
class Mapping:
def func(self):
print('function_in_Mapping')
__func = func
class MappingSubclass(Mapping):
def func(self):
print('function_in_MappingSubclass')
__func = func c = Mapping()
c.func()
e = MappingSubclass()
e.func()
e._Mapping__func()
e._MappingSubclass__func()
E:\Coding\Python>python class_test.py
function_in_Mapping
function_in_MappingSubclass
function_in_Mapping
function_in_MappingSubclass
而直接調(diào)用 c.__func() 會(huì)報(bào)錯(cuò),表明這個(gè)屬性并不存在,因?yàn)橐呀?jīng)被改寫成了 _Mapping__func 或 _MappingSubclass__func
class Mapping:
def func(self):
print('function_in_Mapping')
__func = func
class MappingSubclass(Mapping):
def func(self):
print('function_in_MappingSubclass')
__func = func
c = Mapping()
c.func()
e = MappingSubclass()
e.func()
e._Mapping__func()
e._MappingSubclass__func() c.__func()
e.__func()
E:\Coding\Python>python class_test.py
function_in_Mapping
function_in_MappingSubclass
function_in_Mapping
function_in_MappingSubclass
Traceback (most recent call last):
File "class_test.py", line 16, in <module>
c.__func()
AttributeError: 'Mapping' object has no attribute '__func'
總結(jié)
以上是生活随笔為你收集整理的Python学习小记(4)---class的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓winpe启动盘-(安卓制作winp
- 下一篇: PAT(B) 1087 有多少不同的值(