Antwort: Re: Some language proposals. ['LBBW': checked]
Jeff Epler
jepler at unpythonic.net
Thu Feb 26 08:50:54 EST 2004
On Thu, Feb 26, 2004 at 10:36:17AM +0100, Holger Joukl wrote:
> Well, there is no bug at all, as far as I can see. Runs in my interpreter.
> Cheers
> Holger
I think that the person you were replying to (whose name you kindly
removed, so I have no idea who it was) was trying to refer to this
behavior:
>>> class A: pass
...
>>> def f(): pass
...
>>> A.f = f; assert A.f is f
AssertionFailure
The statement "A.f = f" sets A.__dict__['f'] = f. The statement 'A.f is
f' calls type(A).__getattribute__, which returns an "unbound method
object".
I won't argue that this behavior isn't surprising (since apparently it
surprised somebody) but it's because of this behavior that the following
code works:
class A:
def f(self): pass
a = A()
A.f(a)
.. if A.f returned something other than an unbound method object, then
it would also be legal to call 'A.f(0)' when 0 is not an instance of A.
If the OP wants to add a function to A that is not treated as a normal
method, then he can use the 'staticmethod' wrapper.
>>> A.f = staticmethod(f)
>>> A.f is f
1
Jeff
More information about the Python-list
mailing list