python之特殊成員

1、__init__ :   類()自動執行

2、__call__ :   對象()      類()()   自動執行

class Foo:
    def __init__(self):
        print('init')

    # 對象後面加()就會執行這一部分
    def __call__(self, *args, **kwargs):
        print('call')

obj = Foo()  # init
obj()  # call
Foo()()  #  init  call

3、__int__ :int(對象)和__str__:str()

class Foo:
    def __init__(self):
        pass

    def __int__(self):
        return 1111

    def __str__(self):
        return 'alex'

obj = Foo()
print(obj, type(obj)) 
# int(對象),自動執行對象的__init__方法,並將返回值賦給int對象
r = int(obj)
print(r)
i = str(obj)
print(i)



# 實驗結果
# alex <class '__main__.Foo'>
# 1111
# alex
class Foo:
     def __init__(self, n, a):
         self.name = n
         self.age = a

     # def __str__(self):
     #     return '%s --- %s' % (self.name, self.age)

     
obj = Foo('alex', 18)
print(obj)
# <__main__.Foo object at 0x000002A5070FF358>
class Foo:
     def __init__(self, n, a):
         self.name = n
         self.age = a

     def __str__(self):
         return '%s --- %s' % (self.name, self.age)


obj = Foo('alex', 18)
print(obj)  # 在print內部相當於是print(str(obj)),  obj.__str__,並獲取返回值
# alex --- 18

4、__add__

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __add__(self, other):
        # self = obj1(alex, 19)
        # other = obj2(eric, 66)
        # return self.age+other.age
        # return Foo('tt', 99)
        return Foo(obj1.name, other.age)
obj1 = Foo('alex', 19)
obj2 = Foo('eirc', 66)
# 當個對象相加時,自動執行第一個對象的__add__方法,並且將第二個對象作爲參數傳遞進入
r = obj1 + obj2
print(r, type(r))

5、__del__

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __add__(self, other):
        # self = obj1(alex, 19)
        # other = obj2(eric, 66)
        # return self.age+other.age
        # return Foo('tt', 99)
        return Foo(obj1.name, other.age)

    def __del__(self):
        print('析構方法')  # 對象被銷燬(什麼時候銷燬不知道)時,自從執行

6、__dict__:將對象中封裝的所有內容通過字典的形式返回

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.n = 123

obj = Foo('alex', 18)
d = obj.__dict__
print(d)

# {'name': 'alex', 'age': 18, 'n': 123}
class Foo:
    """
    當前類是幹嘛的,這個註釋也會被當作成員
    """
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.n = 123

ret = Foo.__dict__
print(ret)
# {'__module__': '__main__', '__doc__': '\n    當前類是幹嘛的,這個註釋也會被當作成員\n    ', '__init__': <function Foo.__init__ at 0x0000012292764EA0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>}

7、__getitem__、__setitem__、__delitem__(切片或者索引)

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __getitem__(self, item):
        return item+10

    def __setitem__(self, key, value):
        print(key, value)

    def __delitem__(self, key):
        print(key)

li = Foo('alex', 18)
r = li[8]  # 自動執行li對象的類中的__getitem__方法,8當作參數傳遞給item
print(r)  # 18
li[100] = 123  #  100 123
del li[999]  # 999

索引處理和切片處理

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __getitem__(self, item):
        # 如果item是基本類型,int,str,索引獲取
        # slice 對象,切片
        if type(item) == slice:
            print(item.start)
            print(item.stop)
            print(item.step)
            print('調用者希望內部做切片處理')
        else:
            print('調用者希望內部做索引處理')

    def __setitem__(self, key, value):
        print(key, value)

    def __delitem__(self, key):
        print(key)



li[123]  # 索引  123 <class 'int'>
li[1:4:2]  # 切片  slice(1, 4, 2) <class 'slice'>,將這三個值封裝成一個對象




調用者希望內部做索引處理
1
4
2
調用者希望內部做切片處理

Process finished with exit code 0

8、__iter__

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __iter__(self):
        return iter([11, 22, 33])

li = Foo('alex', 19)
# 如果類中有__iter__方法,對象就是可迭代對象
# 對象.__iter__()的返回值是迭代器
# for 循環,迭代器,next
# for 循環,可迭代對象,對象.__iter__(),迭代器.next
# 1、獲取li對象的類中的__iter__方法,並獲取返回值(迭代器)
# 2、循環上一步中返回的對象
for i in li:
    print(i)

# i,迭代器
# for item in i:
#     print(item)

# i,可迭代對象,執行對象的__iter__方法,獲取迭代器
# for item in i:
    # print(item)

 

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