question about the id()

Skip Montanaro skip at pobox.com
Sun May 15 23:09:18 EDT 2005


    kyo> Can someone explain why the id() return the same value, and why
    kyo> these values are changing?

Instance methods are created on-the-fly.  In your example the memory
associated with the a.f bound method (not the same as the unbound method
A.f) is freed before you reference a.g.  That chunk of memory just happens
to get reused for the bound method associated with a.g.  Here's a
demonstration:

    % python
    Python 2.5a0 (#77, May 14 2005, 14:47:06) 
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class A(object):
    ...   def f(): pass
    ...   def g(): pass
    ... 
    >>> a = A()
    >>> x = a.f
    >>> y = a.g
    >>> id(x)
    17969240
    >>> id(y)
    17969440
    >>> id(a.f)
    17969400
    >>> id(a.g)
    17969400

Skip



More information about the Python-list mailing list