A tuple in order to pass returned values ?

Ulrich Eckhardt ulrich.eckhardt at dominalaser.com
Wed Oct 5 10:31:03 EDT 2011


Am 05.10.2011 15:33, schrieb faucheuse:
> I was wondering something :
> when you do : return value1, value2, value3
> It returns a tuple.

Right.

> So if I want to pass these value to a function, the function have to
> look like :
> def function(self,(value1, value2, value3))
[...]

No, you don't have to, but you can:

# example functions
def fni():
     return 1, 2
def fno(v1, v2):
     pass

# store result in a tuple and unpack tuple for function call
t = fni()
fno(*fni)

# store results in individual values
v1, v2 = fni()
fno(v1, v2)


Note that the first variant can be written in a single line, too. A 
completely different alternative is passing a tuple to the function as a 
single parameter. You can then access the elements using normal tuple 
indexing. That said, I don't see a problem with your syntax, except that 
it's a bit unusual.


Welcome to Python!

Uli



More information about the Python-list mailing list