vendredi 28 août 2020

Why variables assigning works differently when it is used with conditional statement?

Im wondering if:

s = 'foo bar'
a, b = s.split(' ')

print(a)
'foo'
print(b)
'bar'

so why:

s = 'foo bar'
a, b = s.split(' ') if s else '',''

print(a)
['foo', 'bar']
print(b)
''

Can someone explain me why python behaves like shown at the second example?
Thank you!

Aucun commentaire:

Enregistrer un commentaire