views:

79

answers:

2

Try running the following code:

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

Anyone with an explanation as to why None is returned?

+6  A: 

return_method() doesn't return anything. It should return the result of the wrapped func_accepting_args():

def return_method(*args):
    return self.func_accepting_args(prop,*args)
sth
omg! 10 hours wasted from workday .. took less than a minute to solve on stackoverflow :D
Up
+1  A: 

Because return_method() doesn't return a value. It just falls out the bottom, hence you get None.

Peter Rowell