expression in an if statement
nagle at animats.com
Fri Aug 20 08:03:03 CEST 2010
On 8/18/2010 3:12 PM, Thomas Jollans wrote:
> On Wednesday 18 August 2010, it occurred to John Nagle to exclaim:
>> On 8/18/2010 11:24 AM, ernest wrote:
>>> In this code:
>>> if set(a).union(b) == set(a): pass
>>> Does Python compute set(a) twice?
>> CPython does. Shed Skin might optimize. Don't know
>> about Iron Python.
> I doubt any actual Python implementation optimizes this -- how could it? The
> object "set" is clearly being called twice, and it happens to be called with
> the object "a" as a sole argument twice. What if "set" has side effects? A
> compiler could only exclude this possibility if it knew exactly what "set"
> will be at run time, which it can't.
That just reflects the rather lame state of Python implementations.
For some other languages, there are JIT compilers that can optimize such
things. If you rebind a function at run time, the compiled code has to
be invalidated and recompiled with the new binding.
The HotSpot Java JIT compiler did this. See
It's really tough to get this right. There's a huge overhead in
either performance or complexity for allowing rebinding of
functions during execution. It's also a feature very seldom
used after program startup.
I was talking to the Facebook guys doing the compiler for PHP, and they
said that it was a huge win for them that PHP doesn't allow dynamically
replacing a function.
More information about the Python-list