##__del__ 魔术方法(析构方法)
'''
触发时机 对象被内存回收的时候自动出发
功能 对象使用完毕后资源回收
参数 一个self对象
返回值 self
'''
# 基本语法
class Cat():
def __init__(self,name):
self.name = name
def __del__(self):
print("delete")
#1.页面执行完毕回收所有变量
obj = Cat("Tom")
#2.所有对象被del的时候
del obj
# 模拟文件操作
import os
class ReadFile():
def __new__(cls,filename):
if os.path.exists(filename):
return object.__new__(cls)
else:
return print("文件不存在")
def __init__(self,filename):
self.fp = open(filename,mode="r",encoding="utf-8")
def __del__(self):
self.fp.close()
def readcontent(self):
return self.fp.read()
obj = ReadFile("1.py")
print(obj.readcontent())
## __str__ 魔术方法
'''
触发时机 使用print 或者str 的时候触发
功能 查看对象
参数 一个self接受当前对象
返回值 必须返回字符串
'''
class Cat():
gift = "mouse"
def __init__(self,name):
self.name = "Tom"
def cat_gift(self):
return "{},{}".format(self.name,self.gift)
def __str__(self):
return self.cat_gift()
TOM = Cat()
#打印对象
print(obj)
#str(对象)
res = str(TOM)
print(res)
##__repr__ 魔术方法
'''
触发时机 使用repr的时候触发
功能 查看对象
参数 一个self
返回值 必须返回字符串
'''
class Mouse():
gift = "偷"
def __init__(self,name):
self.name = name
def mouse_gift(self):
return "{},{}".format(self.name,self.gift)
def __repr__(self):
return self.mouse_gift()
#系统默认把__repr__方法 赋值给__str__方法
__str__ = __repr__
jerry = Mouse("Jerry")
res = repr(jerry)
print(res)
# ### __call__ 魔术方法
'''
触发时机:把对象当作函数调用的时候自动触发
功能: 模拟函数化操作
参数: 参数不固定,至少一个self参数
返回值: 看需求
'''
# (1) 基本语法
class MyClass():
def __call__(self):
print("__call__魔术方法被触发 ... ")
obj = MyClass()
obj()
# (2) 利用__call__魔术方法做统一调用
class Wash():
def __call__(self,something):
print("我要洗{}".format(something))
self.step1(something)
self.step2()
self.step3()
return "洗完了"
def step1(self,something):
print("放水,把{}扔进去".format(something))
def step2(self):
print("倒洗衣粉")
def step3(self):
print("洗一洗,晾干,穿上")
obj = Wash()
res = obj("袜子")
print(res)
# ### __bool__ 魔术方法
'''
触发时机:使用bool(对象)的时候自动触发
功能:强转对象
参数:一个self接受当前对象
返回值:必须是布尔类型
'''
'''
类似的还有如下等等(了解):
__complex__(self) 被complex强转对象时调用
__int__(self) 被int强转对象时调用
__float__(self) 被float强转对象时调用
...
...
'''
class MyClass():
def __bool__(self):
return True
obj = MyClass()
print(bool(obj))
#__add__ 魔术方法 (与之相关的__radd__ 反向加法)
'''
触发时机:使用对象进行运算相加的时候自动触发
功能:对象运算
参数:二个对象参数
返回值:运算后的值
'''
'''
类似的还有如下等等(了解):
__sub__(self, other) 定义减法的行为:-
__mul__(self, other) 定义乘法的行为:
__truediv__(self, other) 定义真除法的行为:/
...
...
'''
class MyClass():
def __init__(self,num):
self.num = num
# 当对象在 + 号的左侧时,自动触发
def __add__(self,other):
# print(self)
# print(other)
return self.num * 3 + other
def __radd__(self,other):
# print(self) # 对象
# print(other) # 7
return self.num * 5 + other
# add的触发方式
a = MyClass(3)
res = a + 1
print(res)
# radd的触发方式
b = MyClass(5)
res = 7 + b
print(res)
# 对象 + 对象
res = a + b
print(res)
"""
a+b 触发的是add魔术方法 self 接受的是a other 接受的是b
return a.num + b => return 9 + b
res = 9 + b 触发的是radd魔术方法 self 接受的是b other 接受的是9
return b.num * 5 + 9 => 5 * 5 + 9 => 34
"""
# ### __len__ 魔术方法
'''
触发时机:使用len(对象)的时候自动触发
功能:用于检测对象中或者类中某个内容的个数
参数:一个self接受当前对象
返回值:必须返回整型
'''
# len(对象) => 类中的所有自定义成员
class MyClass():
pty1 = 1
pty2 = 2
__pty3 = 3
def func1():
pass
def func2():
pass
def __func3():
pass
def __len__(self):
# 以__开头并且以__结尾的成员过滤掉;
return len( [ i for i in MyClass.__dict__ if not ( i.startswith("__") and i.endswith("__") ) ] )
obj = MyClass()
print(len(obj))