Python quirk in evaluation order

Peter Otten __peter__ at web.de
Fri Jul 31 22:39:25 CEST 2009


James Stroud wrote:

> py> b = 4 if True else b
> py> b
> 4

> Isn't the right side supposed to be evaluated first?

Perhaps it becomes clearer if you change it a bit:


>>> b = 4 if True else whatever
>>> whatever
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'whatever' is not defined

I. e. the else clause is never evaluated at all.

Peter




More information about the Python-list mailing list