How can I create customized classes that have similar properties as 'str'?

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Sat Nov 24 06:05:28 EST 2007


Licheng Fang wrote:
> I find myself frequently in need of classes like this for two
> reasons. First, it's efficient in memory. 

Are you using millions of objects, or MB size objects? Otherwise,
this is no argument.

BTW, what happens if you, by some operation, make a == b, and
afterwards change b so another object instance must be created?
This instance management is quite a runtime overhead.

> Second, when two instances are compared for equality only their
> pointers are compared. 

I state that the object management will often eat more performance
than equality testing. Except you have a huge number of equal
objects. If the latter was the case you should rethink your program
design.

> (I think that's how Python compares 'str's. 

Generally not. In CPython, just very short strings are created only
once.

>>> a=" "
>>> b=" "
>>> a is b
True
>>> a="  "
>>> b="  "
>>> a is b
False
 
Regards,


Björn

-- 
BOFH excuse #430:

Mouse has out-of-cheese-error




More information about the Python-list mailing list