日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Python学习小记(4)---class

發布時間:2023/10/11 综合教程 84 老码农
生活随笔 收集整理的這篇文章主要介紹了 Python学习小记(4)---class 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

 1.名稱修改機制

大概是會對形如 __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, where classname is 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 MappingSubclass were to introduce a __update identifier since it is replaced with _Mapping__update in the Mapping class and _MappingSubclass__update in the MappingSubclass class 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() or eval() does not consider the classname of the invoking class to be the current class; this is similar to the effect of the global statement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies to getattr()setattr() and delattr(), as well as when referencing __dict__ directly.

試驗結果如下

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

而直接調用 c.__func() 會報錯,表明這個屬性并不存在,因為已經被改寫成了 _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'

總結

以上是生活随笔為你收集整理的Python学习小记(4)---class的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲超碰在线 | 国产精品永久免费观看 | 黄色aa级片 | 亚洲国产成人精品视频 | 精品国产乱码久久 | 日日操日日干 | 性午夜| 国产三级第一页 | 人妖干美女 | 国产一区午夜 | 日日噜噜夜夜狠狠久久丁香五月 | 国产情侣在线视频 | 91日本视频 | 久草黄色 | 日韩电影在线观看一区 | 欧美激情久久久久久 | 偷操| 饥渴的少妇和男按摩师 | 中文精品在线 | 国产偷啪| 日韩成人一区二区 | 国产一区在线免费观看 | 怡红院院av| 国产成人99 | 久久久久久久国产精品视频 | 日韩五码电影 | www麻豆| 美女国产毛片a区内射 | 69视频在线播放 | 与亲女洗澡时伦了毛片 | 99精品福利 | 精品一区二区电影 | 国模视频一区二区 | 2017狠狠干 | 97在线观看视频免费 | 久久毛片| 国产激情视频在线 | 深夜影院深a| 丝袜福利视频 | 成人黄色在线网站 | 久久深夜| 狠狠亚洲 | 99在线精品视频 | 亚洲av成人片无码 | 绯色av一区二区 | 少妇精品无码一区二区免费视频 | 日本精品一二三 | 中文在线免费看视频 | 日韩精品国产AV | 91网站免费看 | 精品综合久久久 | 97成人免费视频 | 揄拍自拍 | 国产毛片高清 | 亚洲精品一线二线三线 | 国产123在线 | 天堂资源在线观看 | 伊人黄网| 草在线| 亚洲第一免费 | 国产欧美一区二区三区在线看蜜臀 | 一区二区免费在线视频 | 精品国产一区二区三 | 欧美大黑bbbbbbbbb在线 | 夜夜夜爽 | 邻居少妇张开腿让我爽了在线观看 | 九九热免费在线视频 | 99久久久久久久久久 | 少妇流白浆 | 国产伦精品一区二区三区视频孕妇 | 成人免费毛片男人用品 | 欧美三日本三级少妇三级99观看视频 | 免费在线观看成人av | 欧美xxxxx精品 | 色综合av综合无码综合网站 | 成人亚洲在线 | 天天操天天插天天干 | xxx.国产 | 国产精品九九热 | 香蕉污视频在线观看 | www婷婷| 免费a级片在线观看 | 日韩欧美麻豆 | 欧美一级免费在线 | 这里有精品视频 | 日韩久久精品一区二区 | 97超碰免费观看 | 国产精品一区二区三区线羞羞网站 | 在线97 | 九一亚洲精品 | 操丝袜少妇 | 欧美你懂的 | 丁香婷婷六月天 | 国产八区 | 国产v亚洲v天堂无码久久久 | 不卡影院一区二区 | 日本精品视频一区二区 | 亚洲精品不卡 | 91高清在线视频 |