日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

常用魔法方法

含義

__new__(cls[,...])

1.__new__在對象被實例化時調用

2.第一個參數是類本身,其他參數傳入__init__中

3.__new__如果沒有返回值,則不會調用__init__

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

__init__(self[,..])

1.構造器

__del__(self)

1.析構器

__call__(self[,args...])

1.允許一個類的實例想函數一樣被調用:a(x,y) == a.__call__(x,y)

__len__(self)

1.當被len()時調用

__repr__(self)

1.當被repr()時調用

__str__(self)

1.當被str()時調用

__bytes__(self)

1.當被bytes()時調用

__hash__(self)

1.當被hash()時調用

__bool__(self)

1.當被bool()時調用

__format__(self,format__spec)

1.當被format()時調用

有關屬性

__getattr__(self,name)

1.當用戶試圖獲取不存在的屬性時調用

__getattrbute__(self,name)

1.當類的屬性被訪問時的行為

__setattr__(self,name)

1.當一個屬性被設置時的行為

__delattr__(self,name)

1.當一個屬性被刪除是的行為

__dir__(self)

1.當dir()被調用時的行為

__get__(self,instance,owner)

1.當描述符的值被取得時的行為

__set__(self,insetance,value)

1.當描述符的值被設置時的行為

__delete__(self,instance)

1.當描述符的值被刪除時的行為

比較操作符

__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)

算數運算符

這里的都是從左向右的

__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.當divmod()時調用

__pow__(self,other[,modulo])

1.** 或者 power時調用

__lshift__(self,other)

1.<<

__rshift__(self,other)

1.>>

__and__(self,other)

1.&

__xor__(self,other)

1.^

__or__(self,other)

1. |

反運算

這里的都是從右向左

__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)

增量運算

__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.取絕對值abs()

__invert__(self)

1.取反~

類型轉換

__complex__(self)

1.complex()時調用(需要返回值)

__int__(self)

1.int()時調用(需要返回值)

__float__(self)

1.float()時調用?(需要返回值)

__round__(self[,n])

1.round()時調用?(需要返回值)

__index__(self)

1.當對象是被應用在切片表達式中調用

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

上下文管理with語句

__enter__(self)

1.定義當使用with語句是的初始化行為

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

__exit__(self,exc_type,exc_value,traceback)

1.當代碼塊結束時的行為

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

容器類型

__len__(self)

1.len()時的行為

__getitem__(self,key)

1.獲取容器中指定元素時的行為

__setitem__(self,key,value)

1.設置容器中指定元素時的行為

__delitem__(self,key)

1.刪除容器中指定元素時的行為

__iter__(self)

1.當迭代時的行為

__reversed__(self)

1.reversed()時的行為

__contains__(self,item)

1.in 或 not in 時的行為

總結

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

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