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

Corey Sarsfield subbarker at gmail.com
Fri Jul 12 04:51:58 CEST 2013


Actually, what I wanted was to be able to reference the variable being
assigned to


On Thu, Jul 11, 2013 at 7:07 PM, David Mertz <mertz at gnosis.cx> wrote:

> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>


-- 
Corey Sarsfield
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130711/6a3eeb06/attachment.html>


More information about the Python-ideas mailing list