question about the id()

kyo guan kyoguan at gmail.com
Sun May 15 23:28:31 EDT 2005


HI Skip:

	I want to check is there any change in the instance 's methods.
>>> a=A()
>>> a2=A()
>>> a.f == a2.f
False
>>> a.f is a2.f
False
>>> a.f is a.f
False
>>>
	If the instance methods are create on-the-fly, how to do that? Thanks.

Kyo
 

> -----Original Message-----
> From: Skip Montanaro [mailto:skip at pobox.com] 
> Sent: Monday, May 16, 2005 11:09 AM
> To: kyo guan
> Cc: python-list at python.org
> Subject: Re: question about the id()
> 
> 
>     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