0

I'm new to python and trying to work my way through http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ which has the following line:

result, data = mail.uid('search', None, "ALL") # search and return uids instead

Could someone explain this line?

Thank you.

user1592380
  • 34,265
  • 92
  • 284
  • 515

3 Answers3

7

It means that the function you have called returns an iterable, and the index 0 of the iterable is assigned to x and the index 1 is assigned to y. This is called tuple unpacking.

Eg)

>>> def func(a,b):
...     return b,a
... 
>>> a = 5
>>> b = 7
>>> a,b = func(a,b)
>>> a 
7
>>> b
5 
>>> x = func(a,b)
>>> x
(5, 7)

Edit to show that returning multiple values, they are packed as tuple by default and then unpacked at the other end. Since there is only one variable x here, the tuple is assigned to x.

Simple function for swapping two variables(Just for an example) that answers your question

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
  • 1
    Also true for any iterable, not just tuples. – FatalError Dec 05 '13 at 01:17
  • 1
    It doesn't have to be a tuple. It can be any iterable with exactly as many elements as there are variables to assign to. Tuples are probably the most common, probably followed by lists, but it can even be a generator, a string, or a half-exhausted iterator over a 4-element list. – user2357112 Dec 05 '13 at 01:19
  • Thanks - do all python functions have to return tuples? – user1592380 Dec 05 '13 at 17:25
  • @user61629: If you make a function return more than one variable, it is packed and returned as a tuple. Before assigning, if there are multiple variables at the other end, the tuple is unpacked. – Aswin Murugesh Dec 06 '13 at 05:31
  • Thank you but now I am confused : Does'nt that conflict with what James Wucher said above: "You can return tuples, lists, numbers, strings, class instances, functions, dictionaries, lions, tigers, bears, etc., and "None" " ? – user1592380 Dec 06 '13 at 15:42
  • @user61629: Yes of course. They are sent as a tuple unless and until the type is specified – Aswin Murugesh Dec 07 '13 at 04:36
3

At least, as of python 2.7.x, the function will unpack a tuple of 2 arguments returned from a function. If it returns anything other than 2 arguments in the tuple, I believe it will throw an error if you try to unpack more than this. If it returns 3 arguments and you unpack 2, for example, you will get an exception.

For example:

def func(a):
    return (a,a+1,a*2)

a,b,c = func(7)
print a,b

==> 7 8 # NOTE Values

a = func(3)
print a

==> (3, 4, 6) # NOTE: TUPLE

a,b = func(9)
print a,b

==> Exception - ValueError: too many values to unpack

This may be different in 3.0+.

FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28
  • Thanks, just for my own info do all python functions have to return tuples by convention? – user1592380 Dec 05 '13 at 17:23
  • Nope. You can return tuples, lists, numbers, strings, class instances, functions, dictionaries, lions, tigers, bears, etc., and "None" (the default when you don't explicitly return something). You should know "what" you are getting in the return (i.e. know your API). – FuzzyBunnySlippers Dec 05 '13 at 17:38
1

The other answer, that "the function you have called returns an iterable" is a good one. That is what is happening in your specific example. This is what is called "unpacking" in python. The following are examples of unpacking and assignment related to your question:

>>> a,b = 1,2
>>> a
1
>>> b
2
>>> a,b,c = ['do', 're', 'mi']
>>> a
'do'
>>> b
're'
>>> c
'mi'
>>> 

This is one of the pretty features of Python syntax. If I am not mistaken, it is also optimized - i.e. the fastest way to achieve the result.

mrKelley
  • 3,365
  • 2
  • 21
  • 28