Reputation: 9925
I'm just getting started in python, and either haven't read about this, or missed it, and I don't know what to search for to find my answer.
Playing around with the IMAP module I came across this line of code.
result, data = mail.search(None, "ALL")
What is happening with the two variables here? Is this a syntax that is used when methods return a certain way, or does it always work? Could someone either explain what's going on here, or point me to some documentation?
Upvotes: 2
Views: 186
Reputation: 213311
You can assign multiple variables in Python in one line: -
a, b, c = 1, 2, 3
Assigns three values 1, 2, 3 to a, b, c respectively.
Similarly you can assign values from a list to variables.
>>> li = [1, 2, 3]
>>> a, b, c = li
>>> a
1
>>> b
2
This unpacks your list into 3 variables
Upvotes: 3
Reputation: 2349
This is multiple assignment: the variables result and data simultaneously get the new values returned from mail.search(none, ALL).
The expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.
the multiple assignment documentation is here
http://docs.python.org/tutorial/introduction.html
Upvotes: 0
Reputation: 310069
This is a form of sequence unpacking. If the RHS is an iterable of length 2 (since you have 2 objects on the LHS), you can use it. e.g.:
a,b = (1, 2) #The RHS here is a tuple, but it could be a list, generator, etc.
print a #1
print b #2
Python3 extends this in an interesting way to allow the RHS to have more values than the LHS:
a,b,*rest = range(30)
print(a) #0
print(b) #1
print(rest == list(range(2,30))) #True
Upvotes: 10