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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python引用类 魔法方法_Python 学习笔记 -- 类的魔法方法

發(fā)布時(shí)間:2025/3/21 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python引用类 魔法方法_Python 学习笔记 -- 类的魔法方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

常用魔法方法

含義

__new__(cls[,...])

1.__new__在對(duì)象被實(shí)例化時(shí)調(diào)用

2.第一個(gè)參數(shù)是類本身,其他參數(shù)傳入__init__中

3.__new__如果沒有返回值,則不會(huì)調(diào)用__init__

4.主要用于繼承不可變類型時(shí)重寫

__init__(self[,..])

1.構(gòu)造器

__del__(self)

1.析構(gòu)器

__call__(self[,args...])

1.允許一個(gè)類的實(shí)例想函數(shù)一樣被調(diào)用:a(x,y) == a.__call__(x,y)

__len__(self)

1.當(dāng)被len()時(shí)調(diào)用

__repr__(self)

1.當(dāng)被repr()時(shí)調(diào)用

__str__(self)

1.當(dāng)被str()時(shí)調(diào)用

__bytes__(self)

1.當(dāng)被bytes()時(shí)調(diào)用

__hash__(self)

1.當(dāng)被hash()時(shí)調(diào)用

__bool__(self)

1.當(dāng)被bool()時(shí)調(diào)用

__format__(self,format__spec)

1.當(dāng)被format()時(shí)調(diào)用

有關(guān)屬性

__getattr__(self,name)

1.當(dāng)用戶試圖獲取不存在的屬性時(shí)調(diào)用

__getattrbute__(self,name)

1.當(dāng)類的屬性被訪問時(shí)的行為

__setattr__(self,name)

1.當(dāng)一個(gè)屬性被設(shè)置時(shí)的行為

__delattr__(self,name)

1.當(dāng)一個(gè)屬性被刪除是的行為

__dir__(self)

1.當(dāng)dir()被調(diào)用時(shí)的行為

__get__(self,instance,owner)

1.當(dāng)描述符的值被取得時(shí)的行為

__set__(self,insetance,value)

1.當(dāng)描述符的值被設(shè)置時(shí)的行為

__delete__(self,instance)

1.當(dāng)描述符的值被刪除時(shí)的行為

比較操作符

__lt__(self,other)

1.x < y 等同 x.__lt__(y)

__le__(self,other)

1.x <= y 等同?x.__le__(y)

__eq__(self,other)

1.x == y 等同 x.__eq__(y)

__ne__(self,other)

1.x != y 等同 x.__ne__(y)

__gt__(self,other)

1. x > y 等同 x.__gt__(y)

__ge__(self,other)

1.x >= y 等同 x.__ge__(y)

算數(shù)運(yùn)算符

這里的都是從左向右的

__add__(self,other)

1.+ 從左向右

__sub__(self,other)

1.- ?從左向右

__mul__(self,other)

1.*?從左向右

__truediv__(self,other)

1./ ?從左向右

__floordiv__(self,other)

1.// 從左向右

__mod__(self,other)

1.% 從左向右

__divmod__(self,other)

1.當(dāng)divmod()時(shí)調(diào)用

__pow__(self,other[,modulo])

1.** 或者 power時(shí)調(diào)用

__lshift__(self,other)

1.<<

__rshift__(self,other)

1.>>

__and__(self,other)

1.&

__xor__(self,other)

1.^

__or__(self,other)

1. |

反運(yùn)算

這里的都是從右向左

__radd__(self,other)

__rsub__(self,other)

__rmul__(self,other)

__rtruediv__(self,other)

__rfloordiv__(self,other)

__rmod__(self,other)

__rdivmod__(self,other)

__rpow__(self,other)

__rlshift__(self,other)

__rrshift__(self,other)

__rand__(self,other)

__rxor__(self,other)

__ror__(self,other)

增量運(yùn)算

__iadd__(self,other)

1.+=

__isub__(self,other)

1.-=

__imul__(self,other)

1.*+

__itruediv__(self,other)

1./=

__ifloordiv__(self,other)

1.//=

__imod__(self,other)

1.%=

__ipow__(self,other)

1.**=

__ilshift__(self,other)

1.<<=

__irshift__(self,other)

1.>>=

__iand__(self,other)

1.&=

__ixor__(self,other)

1.^=

__ior__(self,other)

1.|=

一元操作符

__pos__(self)

1.+x

__neg__(self)

1.-x

__abs__(self)

1.取絕對(duì)值abs()

__invert__(self)

1.取反~

類型轉(zhuǎn)換

__complex__(self)

1.complex()時(shí)調(diào)用(需要返回值)

__int__(self)

1.int()時(shí)調(diào)用(需要返回值)

__float__(self)

1.float()時(shí)調(diào)用?(需要返回值)

__round__(self[,n])

1.round()時(shí)調(diào)用?(需要返回值)

__index__(self)

1.當(dāng)對(duì)象是被應(yīng)用在切片表達(dá)式中調(diào)用

2.如果__index__被定義,則__int__也需要被定義,且返回相同的值

上下文管理with語句

__enter__(self)

1.定義當(dāng)使用with語句是的初始化行為

2.__enter__的返回值將賦值給as后面的名字

__exit__(self,exc_type,exc_value,traceback)

1.當(dāng)代碼塊結(jié)束時(shí)的行為

2.一般用來處理異常,或者清除工作,比如關(guān)閉文件之類的

容器類型

__len__(self)

1.len()時(shí)的行為

__getitem__(self,key)

1.獲取容器中指定元素時(shí)的行為

__setitem__(self,key,value)

1.設(shè)置容器中指定元素時(shí)的行為

__delitem__(self,key)

1.刪除容器中指定元素時(shí)的行為

__iter__(self)

1.當(dāng)?shù)鷷r(shí)的行為

__reversed__(self)

1.reversed()時(shí)的行為

__contains__(self,item)

1.in 或 not in 時(shí)的行為

總結(jié)

以上是生活随笔為你收集整理的python引用类 魔法方法_Python 学习笔记 -- 类的魔法方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。