class test :
def fn(self, i):
#test.fn.f = 0 the "compiler" show "not define" errors
#self.fn.f = 0 the "compiler" show "not define" errors
return test.fn.f #ok
return self.fn.f #ok
fn.f = 1
p = test()
print p.fn(1)
I am just curious about why i can’t change the values of a attribute in “fn” method
In essence, it’s…
what differences are between test.fn.f and self.fn.f ? i am sure it’s ok modifing function’s attribute-value , but why i can do that in a method?
What happens is the following:
fn.f = 1gives the function itself an attribute.But on access with
test.fnandself.fn, you don’t get the function itself, but aninstancemethod. Why? Because on attribute access in a class, the component’s__get__method is called if there is any. In the case of functions, this is the case.If you call a function’s
__get__method, you turn it into a bound or unbound instance method which is just a wrapper around the function.You can cope with that with