A tuple in order to pass returned values ?
Dave Angel
davea at ieee.org
Wed Oct 5 11:19:21 EDT 2011
On 01/-10/-28163 02:59 PM, faucheuse wrote:
> 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
More information about the Python-list
mailing list