您好,登錄后才能下訂單哦!
?
目錄
魔術(shù)方法分類:... 1
__dir__. 2
__hash__. 6
__bool__. 10
可視化... 11
?
?
?
魔術(shù)方法:
?
為編程提供便利,可大大提高編程效率;
pythonic;
?
特殊屬性:
__name__,類、函數(shù)、方法等的名字,實例沒有此屬性;
__module__,類定義所在的模塊名;
__class__,對象或類所屬的類,如對象的類的類self.__class__.__class__;
__bases__,類的基類的元組,順序為它們在基類列表中出現(xiàn)的順序,不是mro;
__doc__,類、函數(shù)的文檔字符串,如沒定義為None;
__mro__,類的mro,class.mro()返回的結(jié)果保存在__mro__中,method resolution order,找鏈,mixin;
?
注:
single linkedlist中的__getitem__,僅用于容器,提供一種方便的接口,如索引或其它方式來用;
函數(shù)中的屬性,foo.__defaults__,用元組保存位置參數(shù)默認值;
函數(shù)中的屬性,foo.__kwdefaults__,用元組保存關(guān)鍵字參數(shù)默認值;
?
創(chuàng)建與銷毀:__init__、__del__;
hash;
bool;
__repr__、__str__,可視化;
__add__,運算符重載;
單(雙)向鏈表中的[]、__getitem__、__sizeof__,容器和大??;
可調(diào)用對象,decorator、類裝飾器;
上下文管理,with...as...;
反射(自?。?;
描述器;
其它雜項;
?
?
?
查看屬性,返回類或?qū)ο蟮乃谐蓡T名稱列表;
dir()函數(shù)就是調(diào)用__dir__(),如果提供__dir__(),則返回屬性的列表,否則會盡量從__dict__屬性中收集信息;
如果dir(obj),參數(shù)obj中包含方法__dir__(),該方法將被調(diào)用,如果參數(shù)obj不包含__dir__(),該方法將最大限度的收集參數(shù)信息;
?
dir(),對于不同類型的對象有不同的行為:
如果對象是模塊對象,列表包含模塊的屬性名;
如果對象是類型或類對象,列表包含類的屬性名、及它的基類的屬性名;
否則,列表包含對象的屬性名、它的類的屬性名、類的基類的屬性名;(向上逐級找)
?
例:
In [8]: dir()?? #搜集當(dāng)前模塊
Out[8]:
['In',
?'Out',
?'_',
?'_1',
?'_2',
?'_3',
?'_4',
?'_5',
?'_7',
?'__',
?'___',
?'__builtin__',
?'__builtins__',
?'__doc__',
?'__loader__',
?'__name__',
……
?
例:
class A:
??? def __init__(self,name=18):
??????? self.name = name
?
class B(A):
??? def __dir__(self):?? #實例的,類中的方法要看第1個參數(shù),是self就是實例的
??????? return ['abcdefg']?? #必須返回列表,若寫為return 'abcd'則返回['a','b','c','d']
?
b = B()
print(sorted(dir(b)))
print(sorted(dir(B)))
print(sorted(b.__dict__))
print(sorted(B.__dict__))
print(dir())?? #pycharm中的dir(),__builtins__內(nèi)置函數(shù)等,如果對象是模塊對象,列表包含模塊的屬性名;
?
輸出:
['abcdefg']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
['name']
['__dir__', '__doc__', '__module__']
['A', 'B', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'b']
?
例:
#example_class_magicmethod2.py
class Animal:
??? X = 123
??? def __init__(self,name):
??????? self._name = name
??????? self.__age = 10
??????? self.weight = 20
?
print("animal module's name = {}".format(dir()))?? #如果對象是模塊對象,列表包含模塊的屬性名;
輸出:
animal module's name = ['Animal', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
?
?
# example_class_magicmethod3.py
from example_class_magicmethod2 import Animal
import example_class_magicmethod2
?
class Cat(Animal):
??? X = 'cat'
??? Y = 'abcd'
?
class Dog(Animal):
??? def __dir__(self):
??????? return ['dog']
?
print("current module's name = {}".format(dir()))?? #模塊名詞空間內(nèi)的屬性
print("example_class_magicmethod2 module's name = {}".format(dir(example_class_magicmethod2)))?? #指定模塊名詞空間內(nèi)的屬性
?
print('~~~~~~~~~~~~~~~~~~')
print("object's __dict__ = {}".format(object.__dict__.keys()))?? #祖先類object的字典
print('~~~~~~~~~~~~~~~~~~')
?
print("Animal class's dir() = {}".format(dir(Animal)))?? #類Animal的dir()
print("Cat class's dir() = {}".format(dir(Cat)))?? #類Cat的dir()
print("Dog class's dir() = {}".format(dir(Dog)))
?
print('~~~~~~~~~~~~~~~~~~')
tom = Cat('tom')
print("Cat instance's dir() = {}".format(sorted(dir(tom))))?? #實例的dir(),包括tom實例的屬性、Cat類屬性、祖先類object屬性,sorted返回dict的key組成的列表
print("Cat instance's __dir__ = {}".format(sorted(tom.__dir__())))?? #同上
print('~~~~~~~~~~~~~~~~~~')
?
print(sorted(set(tom.__dict__.keys()) | set(Cat.__dict__.keys()) | set(object.__dict__.keys())))?? #實例的dir()近似等價于該行的__dict__所有(實例的、所屬類的、祖先類的所有屬性)屬性;一般僅用__dict__就夠了,是自己設(shè)計的,dir(tom)能查到所有;
?
print('~~~~~~~~~~~~~~~~~~')
print("Dog class's dir() = {}".format(dir(Dog)))
dog = Dog('snoopy')
print("Dog instance's dir() = {}".format(dir(dog)))
print("Dog instance's __dict__ = {}".format(dog.__dict__))
輸出:
current module's name = ['Animal', 'Cat', 'Dog', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'example_class_magicmethod2']
example_class_magicmethod2 module's name = ['Animal', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
~~~~~~~~~~~~~~~~~~
object's __dict__ = dict_keys(['__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__init__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__', '__doc__'])
~~~~~~~~~~~~~~~~~~
Animal class's dir() = ['X', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
Cat class's dir() = ['X', 'Y', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
Dog class's dir() = ['X', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
~~~~~~~~~~~~~~~~~~
Cat instance's dir() = ['X', 'Y', '_Animal__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_name', 'weight']
Cat instance's __dir__ = ['X', 'Y', '_Animal__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_name', 'weight']
~~~~~~~~~~~~~~~~~~
['X', 'Y', '_Animal__age', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_name', 'weight']
~~~~~~~~~~~~~~~~~~
Dog class's dir() = ['X', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
Dog instance's dir() = ['dog']
Dog instance's __dict__ = {'_name': 'snoopy', '_Animal__age': 10, 'weight': 20}
?
例:
class A:
??? def __init__(self,name=18):
??????? self.name = name
?
class B(A):
??? def __dir__(self):
??????? return ['abcdefg']
?
b = B()
print('~~~~~~~~~~~~~')
print(sorted(dir(b)))
print(sorted(dir(B)))?? #與dir(A)相等
print(sorted(dir(A)))
print('~~~~~~~~~~~~~')
print(sorted(b.__dict__))
print(sorted(A.__dict__))
print(sorted(B.__dict__))
print('~~~~~~~~~~~~~')
print(dir())
print(b.__dict__)
輸出:
~~~~~~~~~~~~~
['abcdefg']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
~~~~~~~~~~~~~
['name']
['__dict__', '__doc__', '__init__', '__module__', '__weakref__']
['__dir__', '__doc__', '__module__']
~~~~~~~~~~~~~
['A', 'B', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'b']
{'name': 18}
?
?
?
?
list源碼中的;
??? @staticmethod?? # known case of __new__ #很少用
??? def __new__(*args, **kwargs):?? ?# real signature unknown
??????? """ Create and return a new object.? See help(type) for accurate signature. """
??????? Pass
??? __hash__ = None?? #技巧,類中不可hashable寫法,不可hash類型就是這樣實現(xiàn)的
?
__hash__,內(nèi)建函數(shù)hash()調(diào)用的返回值,只能返回一個整型(返回一個整數(shù)),如果定義這個方法,該類的實例就可hashable;
一切對象都有__hash__,來自object,object默認還實現(xiàn)了__eq__方法;
__eq__,對應(yīng)==操作符,判斷2個對象是否相等,返回bool值,若返回None則兩個對象相等;
__hash__方法只是返回一個hash值作為set的key,但是去重還需要__eq__來判斷2個對象是否相等;
哈希,時間復(fù)雜度O(1);
?
hash值相等,只是hash沖突,不能說明2個對象是相等的,因此,一般來說,提供__hash__方法是為了作為set或dict的key的,所以去重要同時提供這兩個方法;
類中要兩種同時用,__hash__、__eq__;
?
可hashable對象必須提供__hash__方法,沒有提供的話,isinstance(p1,collections.Hashable)一定為False;
from collections import Hashable
print(Hashable(isinstance(A,Hashable)))?? #很少用,可否hashable用hash()判斷即可
?
注:
元組里有列表則不可hashable;
def __hash__(self),只能返回整型;
一致性hash,hash環(huán),2^32;
def hash(x):
???????? return x % 3?? #return x % 3000000,沖突域大,則沖突可能性小
md5,單向散列,不可逆;
hash,一般在緩存中用,時間復(fù)雜度O(1);
緩沖,大壩,匹配生產(chǎn)者和消費者速度;
n個實例得到的hash值一樣,hash沖突,dict要解決問題,解決辦法:拉鏈法、開地址法;
__eq__,==,def __eq__(self,other): return None,則兩個對象等效(相等);
?
例:
In [1]: o = [1,2,3]
In [2]: hash(o) ??#相當(dāng)于o.__hash__(),等價于None()
---------------------------------------------------------------------------
TypeError???????????????????????????????? Traceback (most recent call last)
<ipython-input-2-5823e6f515e9> in <module>()
----> 1 hash(o)
TypeError: unhashable type: 'list'
In [3]: o.__hash__()
---------------------------------------------------------------------------
TypeError???????????????????????????????? Traceback (most recent call last)
<ipython-input-3-36e00a5ab94a> in <module>()
----> 1 o.__hash__()
TypeError: 'NoneType' object is not callable
?
例:
class A:
??? def __hash__(self):
??????? return 1?? #只能返回整型,否則TypeError: __hash__ method should return an integer
?
print(hash(A()))
輸出:
1
?
例:
class A:
??? __hash__ = None?? #不可hash類型就是這樣實現(xiàn)的
?
print(hash(A()))
輸出:
Traceback (most recent call last):
? File "/home/python/magedu/projects/cmdb/example_class_magicmethod1.py", line 28, in <module>
??? print(hash(A()))
TypeError: unhashable type: 'A'
?
例:
class A:
??? def __init__(self,name=18):
??????? self.name = name
?
??? def __hash__(self):?? #可hashable,hash沖突(hash值一樣)
??????? return 1
?
lst = [A(),A()]
print(lst)
s = set(lst)?? #set還作了is判斷
print(s)
print(len(s))?? #2
for x in s:
??? print(hash(x))
輸出:
[<__main__.A object at 0x7fee3d5f4438>, <__main__.A object at 0x7fee3d5f4b00>]
{<__main__.A object at 0x7fee3d5f4438>, <__main__.A object at 0x7fee3d5f4b00>}
2
1
1
?
例:
class A:
??? def __init__(self,name=18):
??????? self.name = name
?
??? def __hash__(self):?? #可hash
??????? return 1
?
??? def __eq__(self, other):?? #去重,兩個是否相等,相等set才去重;可hash和去重是兩個概念,兩個一塊用
?????????????????? # return False?? #等價于上例,兩個不相等,len(s)為2
??????? # return True
??????? return self.name == other.name
?
a = A(4)
b = A(4)
print(a.__dict__)
print(b.__dict__)
lst = [a,b]
print(lst)
s = set(lst)
print(s)
print(len(s))?? #1
for x in s:
??? print(hash(x))
輸出:
{'name': 4}
{'name': 4}
[<__main__.A object at 0x7f0a3e3ca438>, <__main__.A object at 0x7f0a3e3ca470>]
{<__main__.A object at 0x7f0a3e3ca438>}
1
1
?
例:
設(shè)計二維坐標(biāo)類Point,比較2個坐標(biāo)是否相等?
from collections import Hashable
?
class Point:
??? def __init__(self,x,y):
??????? self.x = x
??????? self.y = y
?
??? def __hash__(self):
??????? return hash((self.x,self.y))
?
??? def __eq__(self, other):
??????? return self.x == other.x and self.y == other.y
?
p1 = Point(4,5)
p2 = Point(4,5)
print(hash(p1))
print(hash(p2))
print(p1 is p2)?? #False
print(p1 == p2)?? #True
print(set((p1,p2)))
print(isinstance(p2,Hashable))?? #True
輸出:
3713084879518070856
3713084879518070856
False
True
{<__main__.Point object at 0x7fd0fc7e6b38>}
True
?
?
?
內(nèi)建函數(shù)bool(),或?qū)ο蠓旁谶壿嫳磉_式的位置,調(diào)用這個函數(shù)返回布爾值;
沒有定義__bool__(),就找__len__()返回長度,非0為真;如果__len__()也沒有定義,那么所有實例都返回真;
__bool__()和__len__()都沒有,恒為True;
?
empty list、empty set、empty tuple、empty dict,四大皆空,集合類型都為空,等效于False;
?
__bool__,調(diào)所有對象,逐級向上找,object中有__bool__的最簡單實現(xiàn),object上也沒,就找__len__,__len__也沒有,恒為True,一切皆為真;
有__len__,說明是個容器,里面沒有東西說明為空,__len__返回值>=0;
?
注:
鏈表中不寫__bool__,只寫__len__,可實現(xiàn)等效False;
?
例:
class A:
??? pass
?
class B:
??? def __bool__(self):
??????? # return False
??????? return True
?
class C:
??? def __len__(self):
?? ?????# return 0
??????? return 1
???
print(bool(A()))
print(bool(B))
print(bool(B()))
print(bool(C()))
print(C().__len__())
print(len(C()))
輸出:
True
True
True
True
1
1
?
?
?
__repr__、__str__;
__repr__,內(nèi)建函數(shù)repr()對一個對象獲取字符串表達,如果一個類定義了__repr__()但有定義__str__,那么在請求該類的實例的“非正式”的字符串表示時也將調(diào)用__repr__();
__str__,str()函數(shù),內(nèi)建函數(shù)format、print()調(diào)用,需要返回對象的字符串表達;
__bytes__,bytes的時候,返回一個對象的bytes表達,即返回bytes對象;
?
__repr__、__str__都沒有,找object(打印內(nèi)存地址);
沒有__str__,找__repr__;
?
沒有__repr__,只有__str__:
str()、format、print使用沒問題;
內(nèi)部打印找object,如print([A()]),顯示[<__main__.A object at 0x7f5ac6059470>];
?
一般__repr__、__str__兩個都要,寫的一模一樣;
技巧,在定義__repr__后,__str__ =? __repr__,類似__hash__ = None;
?
例:
class A:
??? def __init__(self):
??????? self.a = 'a'
??????? self.b = 'b'
?
??? def __repr__(self):
??????? return 'repr: {},{}'.format(self.a,self.b)
?
??? def __str__(self):
??????? return 'str: {},{}'.format(self.a,self.b)
?
print(A())?? #print函數(shù)使用__str__
print([A()])?? #[]使用__str__,但其內(nèi)部使用__repr__
print(([str(A())]))?? #[]使用__str__,str()函數(shù)也使用__str__
print('str:a,b')
s = 'b'
print(['a'],(s,))
輸出:
str: a,b
[repr: a,b]
['str: a,b']
str:a,b
['a'] ('b',)
?
例2:
class A:
??? def __init__(self):
??????? self.a = 'a'
??????? self.b = 'b'
?
??? # def __repr__(self):
??? #???? return 'repr: {},{}'.format(self.a,self.b)
?
??? def __str__(self):
??????? return 'str: {},{}'.format(self.a,self.b)
?
print(A())
print([A()])?? #內(nèi)部調(diào)用__repr__(),__repr__()沒有則調(diào)用object打印內(nèi)存地址
print(([str(A())]))
print('str:a,b')
s = 'b'
print(['a'],(s,))
輸出:
str: a,b
[<__main__.A object at 0x7f5ac6059470>]
['str: a,b']
str:a,b
['a'] ('b',)
?
?
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。