[CentralOH] Syntactic Sugar (or sacharine)?

Brandon Craig Rhodes brandon.craig.rhodes at gmail.com
Fri Aug 12 18:55:07 CEST 2011

Mark Erbaugh <mark at microenh.com> writes:

> I accidentally initialized the dict with {min:0} rather than
> {'min':0}, but the former worked as the built-in function min is
> hashable ... I'm thinking it's marginally faster as there's no string
> processing involved.

An interesting hypothesis!  You should run a test with the "timeit"
module to see whether it is truly faster.

My guess is that it is *not* actually faster, because all of the strings
that appear as literals in your Python code are "interned" strings - no
matter how many times the literal string 'min' appears in your program,
CPython creates only a single string object that gets used over and
over.  And interned strings are, like functions, compared by identity -
the check for d['min'] will, just like d[min], be reduced immediately to
an extremely fast pointer comparison operation.

But run timeit anyway, because I could be wrong! :)

Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon

More information about the CentralOH mailing list