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

Joao S. O. Bueno jsbueno at python.org.br
Fri Jul 12 05:02:43 CEST 2013


On 11 July 2013 21:07, 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:
> > ...
>> 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.

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

(I worked around this particular issue with a couple of helper simple functions
to push the value to a list and return it back )


More information about the Python-ideas mailing list