Python __getattr__()函数的应用
getattr(self, item)函数
class Foo(object):
def __init__(self,name,age):
self.name=name
self.age=age
def show(self):
return '123'
def __getattr__(self, item):
#执行对象中没有的成员时,会执行该函数
print('item',item)
return '999'
obj=Foo('sally',40)
print('show:',obj.show())
#xxx不是obj对象中的成员,所以会触发__getattr__(self,item)函数,并将xxx做为参数。
print('getattr return:',obj.xxx)
输出:
show: 123
item xxx
getattr return: 999
原文地址:https://blog.csdn.net/weixin_43631940/article/details/142626541
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!