Optimising literals away

Benjamin Peterson benjamin at python.org
Mon Aug 30 18:00:13 CEST 2010


Tobias Weber <towb <at> gmx.net> writes:

> 
> Hi,
> whenever I type an "object literal" I'm unsure what optimisation will do 
> to it.
> 
> def m(arg):
>   if arg & set([1,2,3]):
>     return 4
> 
> Is the set created every time the method is called?

Yes, and the list.

> What about a 
> frozenset?

Yep.

> Or tuple vs list?

Tuples containing other immutable literals can be optimized. (In cpython anyway.)

> After how many calls per second does it pay 
> to save it at the module level?

Ask the profiler. Probably not many.

> Would anybody else find this ugly?

I do it all the time. 







More information about the Python-list mailing list