__getattr_uu会一直返回None,即使我尝试返回值

2024-04-25 09:11:27 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试运行以下代码:

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 

有没有人能解释为什么没有人回来?在


Tags: nametestselfnonegetreturnisdef
2条回答

因为return_method()不返回值。它只是从底部掉下来,所以你什么也得不到。在

return_method()不返回任何内容。它应该返回包装的func_accepting_args()的结果:

def return_method(*args):
    return self.func_accepting_args(prop,*args)

相关问题 更多 >