魔法函数的初步认识
1、魔法函数会给类对象增加某些特定功能,并对语法产生影响(例如可遍历或是可用某些特地方法)
2、魔法函数不需要人为去调用,当运行代码时,python会自动调用到相应的魔法函数
非数学运算的魔法函数
字符串表示:__repr__,__str__
集合、序列相关:__len__,__getitem__,setitem__,__delitem__,__contains__
迭代相关:__iter__,__next__
可调用:__call__
with上下文管理器: __enter__,__exit__
数值转换:__abs__,__bool__,__int__,__float__,
元类相关:__init__,__new__
属性相关:__getattr__,__setattr__,__getattribute__,__setattribute__,__dir__
协程:__await__,__aiter__,__anext__,__aenter__,__aexit__
class Company(object):
def __init__(self,employee_list):
self.employee = employee_list
# for循环先找__iter__,没有iter,再找__getitem__
def __iter__(self):
pass
# for循环会找到Company的对象的getitem方法,item会自动从0增长直到对象的列表结束
# 定义了getitem后,Company的对象可迭代
def __getitem__(self,item):
return self.employee[item]
# 当使用len()内置函数时,会首先调用__len__方法,如果没有,则去看有没有__getitem__
def __len__(self):
return len(self.employee)
# __repr__是在jupyter或是python解释器这类开发者模式调用的
def __repr__(self):
return ",".join(self.employee)
# 对对象进行字符串格式化时,调用__str__
def __str__(self):
return ",".join(self.employee)
company = Company(["Tom","Bob","Jane"])
# 先调用__len__,若无,则去看__getitem__
print(len(company))
'''len(dict\list\str等内置函数类型的对象),当使用len()函数对内置类型做len长度计算时,
会直接读取C语言里表示长度的数据,而不需要再次进行计算。因此,应尽量使用python的原生类型,效率远比自己定义的类型高'''
for i in company:
print(i)
数学运算的魔法函数
class Nums(object):
def __init__(self,num):
self.num = num
# 当调用内置函数abs时,会隐含地去调用__abs__魔法函数
def __abs__(self):
return abs(self.num)
num = Nums(-1)
print(abs(num))
class MyVector(object):
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self,other):
re_vector = MyVector(self.x+other.x,self.y+other.y)
return re_vector # 记得返回值
def __str__(self):
return "x:{x},y:{y}".format(x=self.x,y=self.y)
first_vec = MyVector(1,2)
second_vec = MyVector(2,3)
print(first_vec+second_vec) # 调用完__add__后,再调用__str__