python - Setting attributes on __func__ -
in documentation on instance methods states that:
methods support accessing (but not setting) arbitrary function attributes on underlying function object.
but can't seem able verify restriction. tried setting both arbitrary value , 1 of "special attributes" of functions:
class cls: def foo(self): f = self.foo.__func__ f.a = "some value" # arbitrary value f.__doc__ = "documentation" print(f.a, f.__doc__)
when executed, no errors produced , output expected:
cls().foo() # prints out f.a, f.__doc__
what i'm misunderstanding documentation?
you misunderstanding being said. says can access not set attributes of underlying function object from method!
>>> class foo: ... def foo(self): ... self.foo.__func__.a = 1 ... print(self.foo.a) ... self.foo.a = 2 ... >>> foo().foo() 1 traceback (most recent call last): file "<stdin>", line 1, in <module> file "<stdin>", line 5, in foo attributeerror: 'method' object has no attribute 'a'
note how foo.a
updated when set on __func__
value, cannot set directly using self.foo.a = value
.
so function object can modified please, method wrapper provides read-only access attributes on underlying function.
Comments
Post a Comment