question about the id()

Bengt Richter bokr at oz.net
Mon May 16 13:28:31 EDT 2005


On Mon, 16 May 2005 18:30:47 +0200, Peter Dembinski <pdemb at gazeta.pl> wrote:

>Skip Montanaro <skip at pobox.com> writes:
>
>>     kyo> Can someone explain why the id() return the same value, and
>>     kyo> why these values are changing?
>>
>> Instance methods are created on-the-fly.  
>
>So, the interpreter creates new 'point in address space' every time
>there is object-dot-method invocation in program?

Yes, but you can save the result of the obj.method expression (that's what it is,
an expression). E.g.,

     bound_method = obj.method

after that, you can write

     bound_method()

or
     obj.method()

(of course, you can pass arguments too, depending on the signature,
remembering that the "self" instance parameter is already bound in
and does not need to be passed again to a bound method)

The obj.method() call will re-evaluate the obj.method expression,
and the bound_method() call will just call the previously created
bound method.

BTW, a typical performance optimization (not done automatically by python)
is to hoist unchanging-value expressions out of loops, and obj.method is
often such an expression, so you will this strategy when people try
to squeeze extra performance from their programs.

Regards,
Bengt Richter



More information about the Python-list mailing list