Question
Asked By – Vishal
Is it possible to have assignment in a condition?
For ex.
if (a=some_func()):
# Use a
Now we will see solution for issue: Can we have assignment in a condition?
Answer
Why not try it out?
>>> def some_func():
... return 2
...
>>> if (a = some_func()):
File "<stdin>", line 1
if (a = some_func()):
^
SyntaxError: invalid syntax
So, no.
Update: This is possible (with different syntax) in Python 3.8
if a := some_func():
This question is answered By – Jason Hall
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0