二、特殊成员

__init__ 类() 自动执行

__del__ 析构方法

__call__ 对象() 类()() 自动执行

__int__ int(对象)

__str__ str()

__add__

__dict__ 字典,将对象中封装的所有内容通过字典的形式返回

__getitem__ 切片(slice类型)或索引

__setitem__

__delitem__

__iter__ 迭代

  #如果类中有__iter__方法,对象=>>可迭代对象

  #对象,__iter__()的返回值:迭代器

  #for 循环,迭代器,nex

  #for 循环,可迭代对象,对象.__iter__(),迭代器,next

  #1、执行li对象的类F类中的__iter__方法,并获取其返回值

  #2、循环上一步中的返回的对象

__call__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

obj = Foo()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
init

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

obj = Foo()
obj()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
init
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 12, in <module>
    obj()
TypeError: 'Foo' object is not callable

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __call__(self, *args, **kwargs):
        print('call')

obj = Foo()
obj()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
init
call

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __call__(self, *args, **kwargs):
        print('call')

# obj = Foo()
# obj()
Foo()()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
init
call

Process finished with exit code 0

__int__

s = "123"
# s = str('123')
i = int(s)
print(i,type(i))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123 <class 'int'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo():
    def __init__(self):
        pass

obj = Foo()

print(obj,type(obj))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f857d05b3d0> <class '__main__.Foo'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo():
    def __init__(self):
        pass

obj = Foo()

print(obj,type(obj))
r = int(obj)
print(r)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f93190bb3d0> <class '__main__.Foo'>
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 14, in <module>
    r = int(obj)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Foo'

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo():
    def __init__(self):
        pass

    def __int__(self):
        return 1

obj = Foo()

print(obj,type(obj))
r = int(obj)
print(r)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f1524dff3d0> <class '__main__.Foo'>
1

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo():
    def __init__(self):
        pass

    def __int__(self):
        return 1111

obj = Foo()

print(obj,type(obj))
# int,对象,自动执行对象的__int__方法,并将返回值赋值给int对象
r = int(obj)
print(r)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f69912413d0> <class '__main__.Foo'>
1111

Process finished with exit code 0

__str__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo():
    def __init__(self):
        pass

    def __int__(self):
        return 1111

    def __str__(self):
        return 'smoke'

obj = Foo()

print(obj,type(obj))

# int,对象,自动执行对象的__int__方法,并将返回值赋值给int对象
r = int(obj)
print(r)
i = str(obj)
print(i)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
smoke <class '__main__.Foo'>
1111
smoke

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

obj = Foo('smoke',18)
print(obj)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7fe15519f3d0>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __str__(self):
        return self.name

obj = Foo('smoke',18)
print(obj)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
smoke

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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('smoke',18)
print(obj)    #print(str(obj)) str(obj) obj中__str__,并获取其返回值

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
smoke-18

Process finished with exit code 0

__add__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 15, in <module>
    r = obj1 + obj2
TypeError: unsupported operand type(s) for +: 'Foo' and 'Foo'

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __add__(self, other):
        return 123

obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123 <class 'int'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __add__(self, other):
        # sel = obj1(smoke,19)
        # other = obj2('cherry',66)
        return 'xxoo'

obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
#两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数传入
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
xxoo <class 'str'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __add__(self, other):
        return self.age + other.age

obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
85 <class 'int'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __add__(self, other):
        # return self.age + other.age
        return Foo('tt',99)

obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f110935d640> <class '__main__.Foo'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

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

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f9c25a6a640> <class '__main__.Foo'>

Process finished with exit code 0

__del__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __add__(self, other):
        # return self.age + other.age
        # return Foo('tt',99)
        return Foo(obj1.name,other.age)

    def __del__(self):
        print('析构方法')   #对象被销毁()时,自动执行
obj1 = Foo('smoke',19)
obj2 = Foo('cherry',66)

r = obj1 + obj2
print(r,type(r))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
<__main__.Foo object at 0x7f0822e69b50> <class '__main__.Foo'>
析构方法
析构方法
析构方法

Process finished with exit code 0

__dict__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

obj = Foo('smoke',18)

d = obj.__dict__
print(d)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
{'name': 'smoke', 'age': 18, 'n': 123}

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

# obj = Foo('smoke',18)
#
# d = obj.__dict__
# print(d)
ret = Foo.__dict__
print(ret)

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo:
    '''
    当前类是干嘛的。。。
    '''
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.n = 123

# obj = Foo('smoke',18)
#
# d = obj.__dict__
# print(d)
ret = Foo.__dict__
print(ret)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
{'__module__': '__main__', '__doc__': '\n    当前类是干嘛的。。。\n    ', '__init__': <function Foo.__init__ at 0x7fc5d63a71f0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__'
 of 'Foo' objects>}

Process finished with exit code 0

__getitem__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

li = [11,22,33,44]
li = list([11,22,33,44])

r1 = li[3]
print(r1)

li[3] = 666
del li[2]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
44

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

li = Foo('smoke',18)
li[8]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 13, in <module>
    li[8]
TypeError: 'Foo' object is not subscriptable

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

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

li = Foo('smoke',18)
r = li[8]    #自动执行li对象的类中的__getitem__方法,8当作参数传递给item
print(r)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
18

Process finished with exit code 0

__setitem__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

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

li = Foo('smoke',18)
r = li[8]    #自动执行li对象的类中的__getitem__方法,8当作参数传递给item
print(r)

li[100] = 123

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
18
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 19, in <module>
    li[100] = 123
TypeError: 'Foo' object does not support item assignment

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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)

li = Foo('smoke',18)
r = li[8]    #自动执行li对象的类中的__getitem__方法,8当作参数传递给item
print(r)

li[100] = "asdf"

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
18
100 asdf

Process finished with exit code 0

__delitem__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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)

li = Foo('smoke',18)
r = li[8]    #自动执行li对象的类中的__getitem__方法,8当作参数传递给item
print(r)

li[100] = "asdf"

del li[999]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
18
100 asdf
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 24, in <module>
    del li[999]
AttributeError: __delitem__

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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('smoke',18)
r = li[8]    #自动执行li对象的类中的__getitem__方法,8当作参数传递给item
print(r)

li[100] = "asdf"

del li[999]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
18
100 asdf
999

Process finished with exit code 0

索引和切片

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        print(item,type(item))
    def __setitem__(self, key, value):
        print(key,value)

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

li = Foo('smoke',18)
li[123]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123 <class 'int'>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        print(item,type(item))
    def __setitem__(self, key, value):
        print(key,value)

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

li = Foo('smoke',18)
li[123]
li[999] = 'smoke'
del li[234]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123 <class 'int'>
999 smoke
234

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        print(item,type(item))
    def __setitem__(self, key, value):
        print(key,value)

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

li = Foo('smoke',18)
li[123]
li[1:4:2]
li[999] = 'smoke'
del li[234]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123 <class 'int'>
slice(1, 4, 2) <class 'slice'>
999 smoke
234

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        # 如果item是基本类型:int, str, 索引获取
        # slice对象的话,切片
        if type(item) == slice:
            print('调用者希望内部做切片处理')
        else:
            print('调用者希望内部做索引处理')
    def __setitem__(self, key, value):
        print(key,value)
    def __delitem__(self, key):
        print(key)

li = Foo('smoke',18)
li[123]
li[1:4:2]

# class Slice:
#     def __init__(self,a,b,c):
#         self.start = a
#         self.end = b
#         self.step = c
#
# obj = Slice(1,4,2)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
调用者希望内部做索引处理
调用者希望内部做切片处理

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        # 如果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 = Foo('smoke',18)
li[123]
li[1:4:2]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
调用者希望内部做索引处理
1
4
2
调用者希望内部做切片处理

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __getitem__(self, item):
        # return item + 10
        # 如果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 = Foo('smoke',18)
li[123]
li[1:4:2]
li[1:3] =[11,22]
del li[1:3]

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
调用者希望内部做索引处理
1
4
2
调用者希望内部做切片处理
slice(1, 3, None) [11, 22]
slice(1, 3, None)

Process finished with exit code 0

__iter__

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

li = [11,22,33,44]

for item in li:
    print(item)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
11
22
33
44

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

li = [11,22,33,44]
li = list([11,22,33,44])
for item in li:
    print(item)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
11
22
33
44

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

li = Foo('smoke',18)

for i in li:
    print(i)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 14, in <module>
    for i in li:
TypeError: 'Foo' object is not iterable

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __iter__(self):
        return [11,22,33]
li = Foo('smoke',18)
# 1、执行li对象的Foo类中的__iter__方法,并获取其返回值
# 2、循环上一步中返回的对象
for i in li:
    print(i)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py", line 17, in <module>
    for i in li:
TypeError: iter() returned non-iterator of type 'list'

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

i = iter([11,22,33,44])
print(next(i))
print(next(i))
print(next(i))
print(next(i))

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

# i,可迭代对象,执行对象的__iter__方法,获取迭代器
for item in i:
    print(item)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
11
22
33
44

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

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

    def __iter__(self):
        return iter([11,22,33])
li = Foo('smoke',18)
# 如果类中有__iter__方法,就叫可迭代对象
# 对象.__iter__()的返回值,是一个迭代器
# for 循环遇到迭代器执行next()
# for循环遇到可迭代对象,对象.__iter__(),迭代器,next
# 1、执行li对象的Foo类中的__iter__方法,并获取其返回值
# 2、循环上一步中返回的对象
for i in li:
    print(i)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
11
22
33

Process finished with exit code 0

三、metaclass,类的祖宗(原始类)

  a.Python中一切事物都是对象

  b.

  class Foo:

    pass

  obj = Foo()

  # obj是对象

  # Foo类也是一个对象,type的对象
  c.

  类都是type累的对象 type(..)

  "对象"都是累的对象 类()

 

# 声明了一个类

class Foo:

  def function(self):

    print(123)

Foo = type('Foo',(object,),{'func':function})

# type('Foo',(object,),{'func': lambda x: 123}) 声明一个类,类中有一个成员func

Foo = type('Foo',(object,),{'func': lambda x: 123})

obj = Foo()

1、type类中的init方法,然后创建对象

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class Foo(object):
    def func(self):
        print('hello smoke')

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class MyType(type):
    def __init__(self,*args,**kwargs):
        print(123)


class Foo(object,metaclass=MyType):    #创建这个类用Mytype创建
    def func(self):
        print('hello smoke')

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class MyType(type):
    def __init__(self,*args,**kwargs):
        print(123)

    def __call__(self, *args, **kwargs):
        print('456')

class Foo(object,metaclass=MyType):
    def __init__(self):
        pass

    def func(self):
        print('hello smoke')

obj = Foo()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123
456

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:special_members.py
# time:2021/03/02

class MyType(type):
    def __init__(self,*args,**kwargs):
        # self=Foo
        print(123)

    def __call__(self, *args, **kwargs):
        # self=Foo
        r = self.__new__()

class Foo(object,metaclass=MyType):
    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        return '对象'

    def func(self):
        print('hello smoke')

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/special_members.py
123

Process finished with exit code 0

执行流程