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

David Mertz mertz at gnosis.cx
Fri Jul 12 02:07:30 CEST 2013


On Thu, Jul 11, 2013 at 4:57 PM, Joao S. O. Bueno <jsbueno at python.org.br>wrote:

> I don't know if what I miss most is the samething the OP is asking for -
> but the idea of re-using a value retrieved in an expression in the
> same expression - without the need
> to assign to a temporary variable.
>
> Like in:
>   value = expensive_function(b)  if expensive_function(b) else
> default_value
>
> (of course this is a trivial example - but nonetheless it would require an
> extra "if" statement to avoid the double call)
>

How about:

  value = expensive_function(b) or default_value

One call, exact same behavior as you request.  Available since Python 1.0.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130711/0c535770/attachment.html>


More information about the Python-ideas mailing list