metaclass and customization with parameters

Carlos Ribeiro carribeiro at gmail.com
Tue Oct 5 21:49:00 EDT 2004


On Tue, 5 Oct 2004 19:54:16 -0400, Jack Diederich
<jack at performancedrivers.com> wrote:
> On Wed, Oct 06, 2004 at 10:35:01AM +1100, Andrew Durdin wrote:
> > On Tue, 5 Oct 2004 18:57:49 -0300, Gustavo Niemeyer
> > <niemeyer at conectiva.com> wrote:
> > >
> > > That's not true. The def statement is rerun every loop, no matter
> > > what parameters are used. One can check that by issuing:
...
<big snip>
...
> > >>> id(f.method1)
> > 9918464
> > >>> id(f.method2)
> > 9918464
> >
> > Here f.method1 and f.method2 share the same id, which is different to
> > that of f.method1...
> >
> > Can anyone enlighten me as to why this happens?
> 
> A temporary object is being made when you lookup 'method2' in 'f'
> Try assigning the value to a local variable and then running id() on those
> local variables.  The id()s should remain stable. What you are seeing is
> just a fluke, the same id gets used and then scrapped and then used again.

Check this (from the same code snippet you've written), running on my
PC with 2.3:

>>> id(f.method0)
20474736
>>> id(f.method1)
20474736
>>> id(f.method2)
20474736
>>> f.method0.im_func
<function method0 at 0x01733BF0>
>>> f.method1.im_func
<function method1 at 0x01733AB0>
>>> f.method2.im_func
<function method2 at 0x01733330>

I think this is because methods are now implemented as descriptors,
and as such, are not required to return stable id values as one would
expect. The im_func entry is clearly different, even if first part
(the id() listings) seems to corroborate your findings.


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list