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)