[Python-ideas] Combining test and assignment

Eike Hein sho at eikehein.com
Sun Jan 22 05:18:24 CET 2012


On 1/22/2012 5:11 AM, Chris Rebert wrote:
> That doesn't actually work (unless you happen to be writing code in
> module-level scope, where globals are locals):

Right, brainfart - the locals() of test aren't the
locals of the callsite obviously :). And nonlocal
won't work due to the lack of an existing binding
in the outer scope.

Can anyone think of another way to do an assignment
and a truth test in the same expression with today's
tools?


> http://docs.python.org/library/functions.html#locals :
> "Note: The contents of this dictionary should not be modified; changes
> may not affect the values of local and free variables used by the
> interpreter."

Yep, I was actually confusing this with vars() in terms
of whether modifying its return value is considered
kosher. Thanks. Sadly that makes the above harder ...


> Cheers,
> Chris

-- 
Best regards,
Eike Hein



More information about the Python-ideas mailing list