__getattr__ in C++

Gordon McMillan gmcm at hypernet.com
Mon May 15 09:11:33 EDT 2000


David P. Kleinschmidt <dpk7386 at rit.edu> wrote:

>I'm writing a Python module in C++ that provides a number of 
classes.  
>Believe it or not, this is actually working pretty well.  I'm 
wondering, 
>though, should __getattr__ increment the reference count before 
it 
>returns the appropriate attribute, or not?

It should incref the attribute and leave the owner alone.

Imagine a sequence like this:
 newref = a.b # here's your __getattr__
 a = None
Now a's refcount drops. If it drops to 0, it gets deallocated, which 
will decref b. Without an incref in __getattr__, the user would have 
an invalid reference.

- Gordon




More information about the Python-list mailing list