Python __即使在我尝试返回值时,getattr_u;也会一直不返回任何值
请尝试运行以下代码:Python __即使在我尝试返回值时,getattr_u;也会一直不返回任何值,python,metaprogramming,Python,Metaprogramming,请尝试运行以下代码: class Test(object): def func_accepting_args(self,prop,*args): msg = "%s getter/setter got called with args %s" % (prop,args) print msg #this is prented return msg #Why is None returned? def __getattr__(self,name): if name.
class Test(object):
def func_accepting_args(self,prop,*args):
msg = "%s getter/setter got called with args %s" % (prop,args)
print msg #this is prented
return msg #Why is None returned?
def __getattr__(self,name):
if name.startswith("get_") or name.startswith("set_"):
prop = name[4:]
def return_method(*args):
self.func_accepting_args(prop,*args)
return return_method
else:
raise AttributeError, name
x = Test()
x.get_prop(50) #will return None, why?!, I was hoping it would return msg from func_accepting_args
有人解释为什么不返回任何内容吗?return\u method()
不返回任何内容。它应该返回包装的func\u accepting\u args()的结果。
:
return\u method()
不返回任何内容。它应该返回包装的func\u accepting\u args()的结果。
:
因为return_method()不返回值。因为return\u method()不返回值,所以它只是从底部掉出来,所以没有任何结果。。它只是从底部掉下来,所以你什么也得不到。天哪!工作日浪费了10个小时。。不到一分钟就解决了stackoverflow:Domg!工作日浪费了10个小时。。解决stackoverflow:D问题不到一分钟
def return_method(*args):
return self.func_accepting_args(prop,*args)