python引用类 魔法方法_Python 学习笔记 -- 类的魔法方法
常用魔法方法
含義
__new__(cls[,...])
1.__new__在對象被實例化時調(diào)用
2.第一個參數(shù)是類本身,其他參數(shù)傳入__init__中
3.__new__如果沒有返回值,則不會調(diào)用__init__
4.主要用于繼承不可變類型時重寫
__init__(self[,..])
1.構(gòu)造器
__del__(self)
1.析構(gòu)器
__call__(self[,args...])
1.允許一個類的實例想函數(shù)一樣被調(diào)用:a(x,y) == a.__call__(x,y)
__len__(self)
1.當(dāng)被len()時調(diào)用
__repr__(self)
1.當(dāng)被repr()時調(diào)用
__str__(self)
1.當(dāng)被str()時調(diào)用
__bytes__(self)
1.當(dāng)被bytes()時調(diào)用
__hash__(self)
1.當(dāng)被hash()時調(diào)用
__bool__(self)
1.當(dāng)被bool()時調(diào)用
__format__(self,format__spec)
1.當(dāng)被format()時調(diào)用
有關(guān)屬性
__getattr__(self,name)
1.當(dāng)用戶試圖獲取不存在的屬性時調(diào)用
__getattrbute__(self,name)
1.當(dāng)類的屬性被訪問時的行為
__setattr__(self,name)
1.當(dāng)一個屬性被設(shè)置時的行為
__delattr__(self,name)
1.當(dāng)一個屬性被刪除是的行為
__dir__(self)
1.當(dāng)dir()被調(diào)用時的行為
__get__(self,instance,owner)
1.當(dāng)描述符的值被取得時的行為
__set__(self,insetance,value)
1.當(dāng)描述符的值被設(shè)置時的行為
__delete__(self,instance)
1.當(dāng)描述符的值被刪除時的行為
比較操作符
__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ù)運算符
這里的都是從左向右的
__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()時調(diào)用
__pow__(self,other[,modulo])
1.** 或者 power時調(diào)用
__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.取反~
類型轉(zhuǎn)換
__complex__(self)
1.complex()時調(diào)用(需要返回值)
__int__(self)
1.int()時調(diào)用(需要返回值)
__float__(self)
1.float()時調(diào)用?(需要返回值)
__round__(self[,n])
1.round()時調(diào)用?(需要返回值)
__index__(self)
1.當(dāng)對象是被應(yīng)用在切片表達式中調(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é)束時的行為
2.一般用來處理異常,或者清除工作,比如關(guān)閉文件之類的
容器類型
__len__(self)
1.len()時的行為
__getitem__(self,key)
1.獲取容器中指定元素時的行為
__setitem__(self,key,value)
1.設(shè)置容器中指定元素時的行為
__delitem__(self,key)
1.刪除容器中指定元素時的行為
__iter__(self)
1.當(dāng)?shù)鷷r的行為
__reversed__(self)
1.reversed()時的行為
__contains__(self,item)
1.in 或 not in 時的行為
總結(jié)
以上是生活随笔為你收集整理的python引用类 魔法方法_Python 学习笔记 -- 类的魔法方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tushare pro接口_利用tush
- 下一篇: python 下划线转驼峰_json字符