29面向對象4_dir-hash-bool-repr-str

 

目錄

魔術方法分類:... 1

__dir__. 2

__hash__. 6

__bool__. 10

可視化... 11

 

 

 

魔術方法:

 

爲編程提供便利,可大大提高編程效率;

pythonic

 

特殊屬性:

__name__,類、函數、方法等的名字,實例沒有此屬性;

__module__,類定義所在的模塊名;

__class__,對象或類所屬的類,如對象的類的類self.__class__.__class__

__bases__,類的基類的元組,順序爲它們在基類列表中出現的順序,不是mro

__doc__,類、函數的文檔字符串,如沒定義爲None

__mro__,類的mroclass.mro()返回的結果保存在__mro__中,method resolution order,找鏈,mixin

 

注:

single linkedlist中的__getitem__,僅用於容器,提供一種方便的接口,如索引或其它方式來用;

函數中的屬性,foo.__defaults__,用元組保存位置參數默認值;

函數中的屬性,foo.__kwdefaults__,用元組保存關鍵字參數默認值;

 

魔術方法分類:

創建與銷燬:__init____del__

hash

bool

__repr____str__,可視化;

__add__,運算符重載;

單(雙)向鏈表中的[]__getitem____sizeof__,容器和大小;

可調用對象,decorator、類裝飾器;

上下文管理,with...as...

反射(自省);

描述器;

其它雜項;

 

 

 

__dir__

查看屬性,返回類或對象的所有成員名稱列表

dir()函數就是調用__dir__(),如果提供__dir__(),則返回屬性的列表,否則會盡量從__dict__屬性中收集信息;

如果dir(obj),參數obj中包含方法__dir__(),該方法將被調用,如果參數obj不包含__dir__(),該方法將最大限度的收集參數信息;

 

dir(),對於不同類型的對象有不同的行爲:

如果對象是模塊對象,列表包含模塊的屬性名;

如果對象是類型或類對象,列表包含類的屬性名、及它的基類的屬性名;

否則,列表包含對象的屬性名、它的類的屬性名、類的基類的屬性名;(向上逐級找)

 

例:

In [8]: dir()   #蒐集當前模塊

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個參數,是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__內置函數等,如果對象是模塊對象,列表包含模塊的屬性名;

 

輸出:

['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()))   #模塊名詞空間內的屬性

print("example_class_magicmethod2 module's name = {}".format(dir(example_class_magicmethod2)))   #指定模塊名詞空間內的屬性

 

print('~~~~~~~~~~~~~~~~~~')

print("object's __dict__ = {}".format(object.__dict__.keys()))   #祖先類object的字典

print('~~~~~~~~~~~~~~~~~~')

 

print("Animal class's dir() = {}".format(dir(Animal)))   #Animaldir()

print("Cat class's dir() = {}".format(dir(Cat)))   #Catdir()

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返回dictkey組成的列表

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__就夠了,是自己設計的,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}

 

 

 

__hash__

 

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類型就是這樣實現的

 

__hash__,內建函數hash()調用的返回值,只能返回一個整型(返回一個整數),如果定義這個方法,該類的實例就可hashable

一切對象都有__hash__,來自objectobject默認還實現了__eq__方法;

__eq__,對應==操作符,判斷2個對象是否相等,返回bool值,若返回None則兩個對象相等;

__hash__方法只是返回一個hash值作爲setkey,但是去重還需要__eq__來判斷2個對象是否相等;

哈希,時間複雜度O(1)

 

hash值相等,只是hash衝突,不能說明2個對象是相等的,因此,一般來說,提供__hash__方法是爲了作爲setdictkey的,所以去重要同時提供這兩個方法;

類中要兩種同時用,__hash____eq__

 

hashable對象必須提供__hash__方法,沒有提供的話,isinstance(p1,collections.Hashable)一定爲False

from collections import Hashable

print(Hashable(isinstance(A,Hashable)))   #很少用,可否hashablehash()判斷即可

 

注:

元組裏有列表則不可hashable

def __hash__(self),只能返回整型;

一致性hashhash環,2^32

def hash(x):

         return x % 3   #return x % 3000000,衝突域大,則衝突可能性小

md5,單向散列,不可逆;

hash,一般在緩存中用,時間複雜度O(1)

緩衝,大壩,匹配生產者和消費者速度;

n個實例得到的hash值一樣,hash衝突,dict要解決問題,解決辦法:拉鍊法、開地址法;

__eq__==def __eq__(self,other): return None,則兩個對象等效(相等);

 

例:

In [1]: o = [1,2,3]

In [2]: hash(o)   #相當於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類型就是這樣實現的

 

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):   #hashablehash衝突(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

 

例:

設計二維座標類Point,比較2個座標是否相等?

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

 

 

 

__bool__

內建函數bool(),或對象放在邏輯表達式的位置,調用這個函數返回布爾值;

沒有定義__bool__(),就找__len__()返回長度,非0爲真;如果__len__()也沒有定義,那麼所有實例都返回真;

__bool__()__len__()都沒有,恆爲True

 

empty listempty setempty tupleempty dict,四大皆空,集合類型都爲空,等效於False

 

__bool__,調所有對象,逐級向上找,object中有__bool__的最簡單實現,object上也沒,就找__len____len__也沒有,恆爲True,一切皆爲真;

__len__,說明是個容器,裏面沒有東西說明爲空,__len__返回值>=0

 

注:

鏈表中不寫__bool__,只寫__len__,可實現等效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__,內建函數repr()對一個對象獲取字符串表達,如果一個類定義了__repr__()但有定義__str__,那麼在請求該類的實例的“非正式”的字符串表示時也將調用__repr__()

__str__str()函數,內建函數formatprint()調用,需要返回對象的字符串表達;

__bytes__bytes的時候,返回一個對象的bytes表達,即返回bytes對象;

 

__repr____str__都沒有,找object(打印內存地址);

沒有__str__,找__repr__

 

沒有__repr__,只有__str__

str()formatprint使用沒問題;

內部打印找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函數使用__str__

print([A()])   #[]使用__str__,但其內部使用__repr__

print(([str(A())]))   #[]使用__str__str()函數也使用__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()])   #內部調用__repr__()__repr__()沒有則調用object打印內存地址

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',)

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章