Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A tuple in order to pass returned values ?

Reply
Thread Tools

A tuple in order to pass returned values ?

 
 
faucheuse
Guest
Posts: n/a
 
      10-05-2011
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 ?
 
Reply With Quote
 
 
 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      10-06-2011
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 ?
>

There is no problem with that but ppl will usually write something like:

def function(self, a3Tuple):
v1, v2 ,v3 = a3Tuple

In a general manner, ppl will tend to use the minimum arguments
required. However, do not pack values into tuple if they are not related.
A better thing to do would be to use objects instead of tuples, tuples
can serve as lazy structures for small application/script, they can
become harmful in more complexe applications, especialy when used in
public interfaces.

JM


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-07-2011
Jean-Michel Pichavant wrote:

> In a general manner, ppl will tend to use the minimum arguments
> required. However, do not pack values into tuple if they are not related.


How would you return multiple values if not in a tuple?

Tuples are *the* mechanism for returning multiple values in Python. If
you're doing something else, you're wasting your time.


> A better thing to do would be to use objects instead of tuples, tuples
> can serve as lazy structures for small application/script, they can
> become harmful in more complexe applications, especialy when used in
> public interfaces.


First off, tuples *are* objects, like everything else in Python.

If you are creating custom classes *just* to hold state, instead of using a
tuple, you are wasting time. Instead of this:

class Record:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z

result = Record(1, 2, 3)

Just use a tuple or a namedtuple: the work is already done for you, you have
a well-written, fast, rich data structure ready to use. For two or three
items, or for short-lived results that only get used once, an ordinary
tuple is fine, but otherwise a namedtuple is much better:

from collections import namedtuple
result = namedtuple('Record', 'x y z')(1, 2, 3)



--
Steven

 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      10-07-2011
Steven D'Aprano wrote:
> Jean-Michel Pichavant wrote:
>
>
>> In a general manner, ppl will tend to use the minimum arguments
>> required. However, do not pack values into tuple if they are not related.
>>

>
> How would you return multiple values if not in a tuple?
>
> Tuples are *the* mechanism for returning multiple values in Python. If
> you're doing something else, you're wasting your time.
>
>
>
>> A better thing to do would be to use objects instead of tuples, tuples
>> can serve as lazy structures for small application/script, they can
>> become harmful in more complexe applications, especialy when used in
>> public interfaces.
>>

>
> First off, tuples *are* objects, like everything else in Python.
>
> If you are creating custom classes *just* to hold state, instead of using a
> tuple, you are wasting time. Instead of this:
>
> class Record:
> def __init__(self, x, y, z):
> self.x = x
> self.y = y
> self.z = z
>
> result = Record(1, 2, 3)
>
> Just use a tuple or a namedtuple: the work is already done for you, you have
> a well-written, fast, rich data structure ready to use. For two or three
> items, or for short-lived results that only get used once, an ordinary
> tuple is fine, but otherwise a namedtuple is much better:
>
> from collections import namedtuple
> result = namedtuple('Record', 'x y z')(1, 2, 3)
>
>

I don't have access to namedtuple, working with python 2.5
It sounds to me that namedtuple exactly tries to fix what I dislike in
tuples : undocumented packing of unrelated data.

However, I'm not sure it fixes the main issue: unpacking. Unpacking
prevents you from adding any additional fields to your 'tuple' without
breaking any line of code that was unpacking the tuple (to oppose to
accessing an object attribute). And it did annoy me a lot when improving
applications. Now I'm using tuples only in small applications, and try
to avoid unpacking as much as possible.

namedtuple sounds great (if you don't use unpacking ) ), too bad it is
available only from python 2.6.

JM


 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      10-10-2011
Jean-Michel Pichavant <jeanmic...@sequans.com> wrote:
> However, I'm not sure it fixes the main issue: unpacking. Unpacking
> prevents you from adding any additional fields to your 'tuple' without
> breaking any line of code that was unpacking the tuple (to oppose to
> accessing an object attribute).


Generally, if it's a small, known, unlikely-to-change structure, I'd
use a tuple. For anything else I'd use a class, namedtuple or bunch.

However, pre-namedtuples I'd usually abstract away the field
referencing with indices and lambdas:

name = 0
role = 1
name_role = lambda t: (t[name], t[role])

name, role = name_role(record)

etc
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      10-10-2011
In article
<e8a3d9d1-7eb8-43c5-9ee1->,
alex23 <> wrote:

> For anything else I'd use [...] bunch.


Particularly useful for handing over lupins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A tuple in order to pass returned values ? faucheuse Python 4 10-05-2011 03:19 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57