Best way to string compare a python object in a getattrofunc?

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Mar 1 19:40:38 EST 2001


Cayce Ullman wrote:
> I am
> thinking about interning the string "spam" on module initialization, then
> saving that pointer somewhere, and then just comparing the pointers on every
> fetch.

Interning your string on initialisation is a good idea,
BUT you should use PyObject_Compare() to compare it with
other strings. If the string you're comparing it with
also happens to be interned, PyObject_Compare() will do
a pointer comparison, otherwise it will use strcmp().
So you'll get the benefits of interning where possible,
while still doing the right thing in other cases.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list