I can do an assignment destructuring as:
a, b = s.split(' ', 1)
for a string s
which has more than one word.
How can we do the same in, say an if
or elif
, with the latest assignment expression introduced in Python 3.8 (is it possible to have multiple targets) ?
I tried:
if some_thing:
# some code.
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
# some code probably using a and b as well.
I get following error:
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
NameError: name 'a' is not defined
The reason I want this is because I don't want to split my string unnecessarily if my first condition is satisfied.