自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python中的下劃線和魔方方法

開發(fā) 后端
雙下劃線一般被用于 Python 內(nèi)置的特殊方法或者屬性,比如 __name__,__file__,有時(shí)候也被稱之為魔方方法。

[[416081]]

下劃線在 Python 中有很特別的意義。

開門見山

下劃線在 Python 中有特殊的意義,簡單來說,可以總結(jié)成三點(diǎn)。

  1. 單下劃線在前一般用于聲明私有成員,比如 _private_var
  2. 單下劃線在后一般用于命名已經(jīng)被保留關(guān)鍵字占用的變量,比如 class_,type_
  3. 雙下劃線一般被用于 Python 內(nèi)置的特殊方法或者屬性,比如 __name__,__file__,有時(shí)候也被稱之為魔方方法。

更多細(xì)節(jié)的討論,可以看 StackOverflow 上的這個(gè)主題:What is the meaning of single and double underscore before an object name?[1]。

__foo__: this is just a convention, a way for the Python system to use names that won't conflict with user names.

_foo: this is just a convention, a way for the programmer to indicate that the variable is private (whatever that means in Python).

__foo: this has real meaning: the interpreter replaces this name with _classname__foo as a way to ensure that the name will not overlap with a similar name in another class.

No other form of underscores have meaning in the Python world.

There's no difference between class, variable, global, etc in these conventions.

有時(shí)候我們還能看到就僅僅命名為一個(gè)下劃線的的變量,這種情況一般是這個(gè)變量不重要或者只是一個(gè)臨時(shí)工,連名字都不配擁有。

  1. for _,value in func(): # 假如func每次會(huì)返回兩個(gè)值,我們只關(guān)心第二個(gè)值 
  2.     use(value) 

思維導(dǎo)圖

下面是思維導(dǎo)圖的總結(jié)

如何調(diào)用魔法方法

一些魔法方法直接和內(nèi)建函數(shù)對應(yīng),這種情況下,如何調(diào)用它們是顯而易見的。這有個(gè)附錄可以作為調(diào)用魔法方法的參考。

魔法方法 什么時(shí)候被調(diào)用 解釋
__new__(cls [,...]) instance = MyClass(arg1, arg2) __new__在實(shí)例創(chuàng)建時(shí)調(diào)用
__init__(self [,...]) instance = MyClass(arg1,arg2) __init__在實(shí)例創(chuàng)建時(shí)調(diào)用
__cmp__(self) self == other, self > other  進(jìn)行比較時(shí)調(diào)用
__pos__(self) self 一元加法符號(hào)
__neg__(self) -self 一元減法符號(hào)
__invert__(self) ~self 按位取反
__index__(self) x[self] 當(dāng)對象用于索引時(shí)
__nonzero__(self) bool(self) 對象的布爾值
__getattr__(self, name) self.name #name 不存在 訪問不存在的屬性
__setattr__(self, name) self.name = val 給屬性賦值
__delattr__(self, name) del self.name 刪除屬性
__getattribute__(self,name) self.name 訪問任意屬性
__getitem__(self, key) self[key] 使用索引訪問某個(gè)元素
__setitem__(self, key) self[key] = val 使用索引給某個(gè)元素賦值
__delitem__(self, key) del self[key] 使用索引刪除某個(gè)對象
__iter__(self) for x in self 迭代
__contains__(self, value) value in self, value not in self 使用 in 進(jìn)行成員測試
__call__(self [,...]) self(args) “調(diào)用” 一個(gè)實(shí)例
__enter__(self) with self as x: with 聲明的上下文管理器
__exit__(self, exc, val, trace) with self as x: with 聲明的上下文管理器
__getstate__(self) pickle.dump(pkl_file, self) Pickling
__setstate__(self) data = pickle.load(pkl_file) Pickling
 

如果你還想了解關(guān)于魔方方法的更多細(xì)節(jié),那么你一定不能錯(cuò)過:

  • 魔法方法指南 [2]
  • Magic method guide[3]

參考資料

[1]What is the meaning of single and double underscore before an object name?: http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python

[2]魔法方法指南: http://pyzh.readthedocs.io/en/latest/python-magic-methods-guide.html

[3]Magic method guide: http://www.rafekettler.com/magicmethods.html

責(zé)任編輯:武曉燕 來源: 哎媽呀Bug
相關(guān)推薦

2023-11-29 18:11:17

Python代碼

2023-11-09 08:55:17

Python雙下劃線

2021-07-31 19:21:34

Python下劃線數(shù)值

2010-03-04 10:35:51

Python下劃線

2024-08-20 16:32:37

python接口自動(dòng)化

2021-12-26 22:55:26

Python下劃線編程

2020-09-22 09:41:48

Python下劃線開發(fā)

2024-08-16 22:00:38

2013-07-24 19:27:45

iOS開發(fā)學(xué)習(xí)自定義帶下劃線文本UI

2020-10-24 11:09:35

Python下劃線用法

2014-05-08 10:36:59

CSS單詞連字符

2022-12-19 15:12:34

python運(yùn)算符

2021-10-20 07:36:03

Python構(gòu)造方法

2010-01-20 14:13:33

C++變量

2016-10-27 15:58:35

魔術(shù)方法PHP

2024-09-26 10:44:53

MySQL數(shù)據(jù)庫數(shù)據(jù)

2009-07-10 14:58:13

JLabel組件JFC和Swing

2010-11-03 10:31:51

PHP魔術(shù)方法

2023-11-30 08:21:33

2015-07-22 17:21:34

Oracle數(shù)據(jù)字典
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)