[Python-ideas] Conditional Assignment in If Statement

Sven R. Kunze srkunze at mail.de
Fri Oct 21 14:26:33 EDT 2016


On 18.10.2016 00:11, Michael duPont wrote:
> What does everyone think about:
>
> if foo = get_foo():
>      bar(foo)
>
> as a means to replace:
>
> foo = get_foo()
> if not foo:
>      bar(foo)
> del foo
>
> Might there be some better syntax or a different keyword? I constantly run into this sort of use case.

Before really understanding what you need here I have some questions:

1) What does real-world code look like here exactly?
2) Why do you need foo to be deleted after the if?
3) Do you need this in interactive sessions, short-lived code or 
maintained code?


Cheers,
Sven


More information about the Python-ideas mailing list