expression in an if statement

Peter Otten __peter__ at web.de
Wed Aug 18 14:34:17 EDT 2010


ernest wrote:

> In this code:
> 
> if set(a).union(b) == set(a): pass
> 
> Does Python compute set(a) twice?

>>> a = "abc"
>>> b = "def"
>>> _set = set
>>> def set(x):
...     print "computing set(%r)" % x
...     return _set(x)
...
>>> if set(a).union(b) == set(a): pass
...
computing set('abc')
computing set('abc')

So yes, set(a) is computed twice.

Peter



More information about the Python-list mailing list