A tuple in order to pass returned values ?

F

faucheuse

Hi, (new to python and first message here \o/)

I was wondering something :
when you do : return value1, value2, value3
It returns a tuple.

So if I want to pass these value to a function, the function have to
look like :
def function(self,(value1, value2, value3)) #self because i'm working
with classes

I tried it, and it works perfectly, but I was wondering if it's a good
choice to do so, if there is a problem by coding like that.

So my question is : Is there a problem doig so ?
 
U

Ulrich Eckhardt

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
 
D

Dave Angel

Hi, (new to python and first message here \o/)

I was wondering something :
when you do : return value1, value2, value3
It returns a tuple.

So if I want to pass these value to a function, the function have to
look like :
def function(self,(value1, value2, value3)) #self because i'm working
with classes

I tried it, and it works perfectly, but I was wondering if it's a good
choice to do so, if there is a problem by coding like that.

So my question is : Is there a problem doig so ?
In the abstract, no. There's no relationship between the two, except
they happen to use the same name in their respective local namespaces.

In practice, I wouldn't do it. If the three values really comprise one
"thing" then it makes sense for a function to expect a single thing, and
that thing needs a name. So I'd define the function as

def function(self, mything):
interesting, useful, related = mything
... work on them

But it's certainly possible that the writer of the first function really
had three independent things to return, and if the second method is
expecting those same three independent things, he should define the
method as:

def function(self, this, that, theother):

Python does have magic syntax to make this sort of thing easier to work
with, using * and **. But I seldom use them unless forced to by
meta-concerns, such as passing unknown arguments through one method to a
method of a superclass.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top