Understanding Python Code

I work through access through Gmail using imaplib and stumbled upon:

# Count the unread emails
status, response = imap_server.status('INBOX', "(UNSEEN)")
unreadcount = int(response[0].split()[2].strip(').,]'))
print unreadcount

I just want to know that:

status,

does before "response =". I would do it, but I have no idea that I even ask you to find an answer for this: (.

Thank.

+5
source share
3 answers

When a function returns a tuple, it can be read by more than one variable.

def ret_tup():
    return 1,2 # can also be written with parens

a,b = ret_tup()

a and b are now 1 and 2 respectively

+13
source

See this page: http://docs.python.org/tutorial/datastructures.html

Section 5.3 refers to “multiple assignment” as “sequence unpacking”

, imap_server , python , .

tuple = imap_server.status('INBOX', "(UNSEEN)")
status = tuple[0]
response = tuple[1]

, , . .

+5

While the answers are certainly sufficient, a quick application of this python function is the ease of replacing values.

In a regular language, to exchange the values ​​of variables x, yyou will need a temporary variable

z = x
x = y
y = z

but in python we can shorten this to

x, y = y, x
+2
source

All Articles