[Python-ideas] Reference variable in assignment: x = foo(?)

David Mertz mertz at gnosis.cx
Fri Jul 12 07:05:37 CEST 2013


On Jul 11, 2013 8:02 PM, "Joao S. O. Bueno" <jsbueno at python.org.br> wrote:
> >
> >   value = expensive_function(b) or default_value
> >
> > One call, exact same behavior as you request.  Available since Python
1.0.
>
> Because, as a I said, that was a naive example - if the predicate was:
>   value = expensive_function(b)  if expensive_function(b)  > threshold
> else  default_value
>
> There is no way "or" would work -
> Anyway - just forget about it -let's see where this e-mail thread leads

value = ([v for v in [expensive_function(b)] if v > threshold] or
[default_value])[0]

I admit it's not the most elegant, but it works generically.

Or just use a temp variable. No shame in doing so.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130711/d77d5ba0/attachment.html>


More information about the Python-ideas mailing list