[Python-Dev] redefining is (original) (raw)

Ilya Sandler ilya at bluefir.net
Thu Mar 18 12:53:25 EST 2004


> The observation is that if an object is immutable, there's > no legitimate reason to know whether it's distinct from > another object with the same type and value.

Here is a real life example where "is" on immutable objects is very helpful for perfomance reasons

I have DatabaseRecord objects which have a field "schema": (which comes from Database objects)

schema is just a string with textual description of database schema and this string might be quite long (like 500-700 bytes)

given 2 databaserecord objects I want to be able to quickly say whether they have the same schema..(with time cost being independent on number of field, record size, etc)

It's easy to achieve with is, I just intern schemas: database.schema=intern(schema) dbRec1.schema=database.schema dbRec2.schema=database.schame

(Essentially, intern() is done once per database open() operation)

then comparing 2 schemas is much quicker done with "is"

(dbRec1.schema is dbRec3.schema)

than with either id() or "=="

Or am I missing something here?

Ilya

PS and just for reference some timings:

src>./python Lib/timeit.py -n 10000 -s 'l=500; x="1" * l; y="1"* l' 'x is y' 10000 loops, best of 3: 0.143 usec per loop

src>./python Lib/timeit.py -n 10000 -s 'l=500; x="1" * l; y="1"* l' 'x == y' 10000 loops, best of 3: 0.932 usec per loop

src>./python Lib/timeit.py -n 10000 -s 'l=500; x="1" * l; y="1"* l' 'id(x) == id(y)' 10000 loops, best of 3: 0.482 usec per loop s

Unsubscribe: http://mail.python.org/mailman/options/python-dev/ilya%40bluefir.net



More information about the Python-Dev mailing list