Python魔术方法
__init__
类似于构造器
#__init__ magic
class Human:
def __init__(self, name):
#print('init exec')
self.sex = 'man'
self.age = 1
self.name = name
def eat(self):
print('eating')
def run(self):
print('running')
def sleep(self):
print('sleeping')
#initilize
one = Human('Tom')
print(one.__dict__)#__dict__打印属性名及属性值构成的字典
########################################
{'sex': 'man', 'age': 1, 'name': 'Tom'}
self
代表对象本身,跟c++的this指针一样自动传入的,后面的参数在实例化之后传入。
one = Human()这一句实际上可以看作执行了两个魔术方法,第一个是new,然后是init,先创建对象,再初始化它。
总结:
- 触发时机:实例化对象之后触发
- 作用:为对象添加对象的所属成员
- 参数:一个self接受当前对象,其他的参数根据实例化的传参决定
- 返回值:无
- 注意事项:无
__new__
创建对象时触发:
#new magic
class Human:
def __new__(cls, sex):
#print(sex)
if sex == 'male':
return object.__new__(cls)#cls换成Human也行
else:
pass
def eat(self):
print('eating')
def run(self):
print('running')
def sleep(self):
print('sleeping')
#initilize
one = Human('male')
if one is not None:
print(one)
#print(one.__dict__)
##########################################
<__main__.Human object at 0x7f73dac7db50>
接受的参数cls代表类本身,当然也可以传类名。接受的其他参数和init魔术方法相同。有返回值,返回生成的对象,若没有,则返回None。
总结:
- 触发时机:实例化对象的时候触发!
- 作用:管理控制对象的生成过程
- 参数:一个cls接受当前类,其他参数同init
- 返回值:可有可无,若无,则实例化结果为None
- 注意事项:new魔术方法和init魔术方法参数一致
猴子偷人
class Monkey:
pass
class Human:
def __new__(cls):
return object.__new__(Monkey)
pass
one = Human()
print(one)
实例化Human类,实例化出来的却是个猴子。
__del__
类似析构函数
class Human:
def __del__(self):
print("del exec")
one = Human()
two = one
del one
del two
print("-==-=-=-=-=-=-=")
- 触发时机:对象被系统回收的时候自动触发
- 作用:回收程序使用过程的信息和变量等
- 参数:就一个self接收当前对象
- 返回值:无
- 注意事项:创建对象并与引用关联后,可能会有多个关联的引用,只有删除(使用内置del方法)到最后一个引用(或者脚本运行至结尾)时才会删除。而由于del魔术方法中没有真正的删除变量机制,执行该函数并不删除变量。只是一个被删除时自动调用的函数。
__call__
将对象名当作函数执行
#制作蛋糕的类
class MakeCake:
#和面
def huomian(self):
print('hou mian')
#发酵
def fajiao(self):
print('fa jiao')
#烘烤
def hongkao(self):
print('hong kao')
#切
def qie(self):
print('qie')
#抹奶油
def monaiyou(self):
print('monaiyou')
#放水果
def fangshuiguo(self):
print('fang shui guo')
#打包
def dabao(self):
print('da bao')
def __call__(self, flavor):
# 用制作蛋糕对象制作蛋糕
self.huomian()
self.fajiao()
self.hongkao()
self.qie()
self.monaiyou()
self.fangshuiguo()
self.dabao()
print('Make a {} flavor cake.'.format(flavor))
#制作一个蛋糕(制作蛋糕的对象)
dg = MakeCake()
dg('chocolate')
################################hou mian
fa jiao
hong kao
qie
monaiyou
fang shui guo
da bao
Make a chocolate flavor cake.
- 触发时机:将对象当作函数调用的时候自动触发
- 作用:常用于归结类/对象的操作步骤,方便后期调用
- 参数,一个self,其余自选
- 返回值:可有可无
- 注意事项:无
__len__
可以提供给内建函数len进行计数的魔术方法
class Car:
#attributes
color = 'black'
weight = '2T'
grand = 'aoliao'
wheels = ['front left', 'front right', 'back left', 'back right', 'spare tire']
def __len__(self):
#print('exec len')
#返回轮子个数
return len(self.wheels)
#methods
def playMusic(self):
print('play music')
def move(self):
print('moving')
#initialize
myCar = Car()
#可以使用len函数检测对象吗
ret = len(myCar)
print("ret = %d" % ret )
- 触发时机:使用内建len函数检测对象时自动触发
- 作用:使得len可以检测对象中某个数据的个数
- 参数,一个self,不可添加别的,要与内建函数len一致
- 返回值:必须有且必须是一个整型数
- 注意事项:len检测什么是由程序员自己决定的
__str__
定义打印对象显示的信息
思考一下:print打印列表元组或者字典等数据时,打印出来的是数据的内容,而自己定义的类进行打印时,如果没有定义str魔术方法,则打印出来的时对象在内存中的信息,这里就介绍让自己的对象被打印时打印类的数据的方法
class Human:
#attributes
color = 'y'
sex = 'female'
age = 18
name = 'cuihua'
#method
#继承自object类,这里是重载
def __str__(self):
return self.name
def eat(self):
print('yummy')
def smile(self):
print('laugh laugh')
cuihua = Human()
print(cuihua)
#触发str的第二个情况
str(cuihua)
- 触发时机:使用print打印对象时自动出发
- 作用:使得len可以检测对象中某个数据的个数
- 参数,一个self
- 返回值:必须有且必须是字符串类型
- 注意事项:除了print外,使用str转换数据类型时也会触发
__repr__
class Human:
#attributes
color = 'y'
age = 18
name = 'flower'
#methoc
def __repr__(self):
print('exec repr')
return self.name
#所有类都默认存在一个等式
#__str__ = __repr__
#__repr__被重载,相当于__str__也被重载
def eat(self):
print('eating')
def drink(self):
print('drinking')
#initialize
flower = Human()
print(flower)
mySong = 'I\nlike\tyou'
#正常打印字符串
print(str(mySong))
#使用repr转换并打印
print(repr(mySong))
- 触发时机:在使用内建函数repr时自动触发
- 作用:可以设置repr函数操作对象的结果
- 参数,一个self
- 返回值:有一个,且必须是串
- 注意事项:正常情况下,类中的str和repr魔术方法是完全一样的(字符串中的两个不一样)
repr和str的对比
__repr__ | __str__ |
---|---|
意义明确无歧义 | 更易读 |
debug一般选repr | 一般不用作debug |
可以传值给eval() | 不能传值给eval() |
调用__repr__ | 调用__str__ |