call仿函数
# ### __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()
# obj.step1()
# obj.step2()
# obj.step3()
res = obj("袜子")
print(res)
标签:__,obj,函数,self,call,something,print
From: https://www.cnblogs.com/wbcde116/p/18078529