weakref confusion

Jeff Epler jepler at unpythonic.net
Wed Feb 25 16:49:57 EST 2004


On Wed, Feb 25, 2004 at 06:36:59PM +0100, Mathias Mamsch wrote:
> Hi,
> 
> I have some confusion concerning the weakref module.

This is because the class dictionary holds a '<function Test>', but the
expression 'A.Test' returns an '<unbound method A.Test>'.  You can
understand why this is the case by looking at what happens in a
subclass:
    >>> class A:
    ...     def m(): pass
    >>> class B(A): pass
    >>> A.m, B.m
    (<unbound method A.m>, <unbound method B.m>)
    >>> A.__dict__['m']
    <function m at 0x813adfc>

just like bound methods on instances, unbound methods are created "on
demand", and are tied to the class that the attribute reference was
performed on.

Jeff




More information about the Python-list mailing list