Re: Using tuples correctly?

Discussion in 'Python' started by =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Oct 11, 2004.

  1. > > So you could write stuff like this:
    > >
    > > return (r: 10, g: 20, b: 30) or maybe return (.r 10, .g 20, .b 30)

    >
    > color = RGB(10, 20, 30)
    > print "The red value is: ", color.red
    >
    > If you *must* have a tuple to throw around, give your class a 'tuple' method:
    >
    > class RGB(object):
    > def __init__(self, r=0, g=0, b=0):
    > self.red = r
    > self.green = g
    > self.blue = b
    >
    > def tuple(self):
    > return (self.r, self.g, self.b)
    >
    > Why bother to shoehorn all of the name scaffolding into tuples when it's already present in classes?


    Because you have to declare classes before you can instantiate them.
    If you wanted a RGB tuple, would you really create a class for it? If
    you wanted a 2d point? I guess tuples aren't really needed in python,
    you can always get by by creating objects when you would have used
    types. However, creating a class when all you want is a C-like struct
    leads to alot of tedious and unnecessary work.

    In my thinking, this:

    return (r: 10, g: 20, b: 30)

    would be read more as "create and return an anonymous object with
    three attributes r, g, b set to 10, 20, 30". But maybe that solves a
    problem that doesn't exist? Either people never use tuples and always
    instantiate classes instead. OK. Fine. Or they use tuples because they
    are convenient and becomes annoyed because they have to use index
    access.
    --
    mvh Björn
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Oct 11, 2004
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

    Ville Vainio Guest

    >>>>> "Bjorn" == BJörn Lindqvist <> writes:

    Bjorn> instantiate classes instead. OK. Fine. Or they use tuples
    Bjorn> because they are convenient and becomes annoyed because
    Bjorn> they have to use index access.

    Did you check out the Bunch data type already?

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

    It doesn't support "tuple unpacking", but otherwise it might fit your
    needs...

    --
    Ville Vainio http://tinyurl.com/2prnb
     
    Ville Vainio, Oct 11, 2004
    #2
    1. Advertising

  3. BJörn Lindqvist wrote:
    > Because you have to declare classes before you can instantiate them.
    > If you wanted a RGB tuple, would you really create a class for it? If
    > you wanted a 2d point? I guess tuples aren't really needed in python,
    > you can always get by by creating objects when you would have used
    > types. However, creating a class when all you want is a C-like struct
    > leads to alot of tedious and unnecessary work.


    My C is a little (a lot) rusty, but don't structures have to be declared
    in pretty much the same way as Python classes? What's the difference?
     
    Leif K-Brooks, Oct 11, 2004
    #3
  4. On Mon, 11 Oct 2004 09:53:18 -0400, Leif K-Brooks
    <> declaimed the following in comp.lang.python:

    >
    > My C is a little (a lot) rusty, but don't structures have to be declared
    > in pretty much the same way as Python classes? What's the difference?


    Heck, in C++ the only difference between a struct and a class is
    that the former defaults to public access of components...

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Oct 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

    Using tuples correctly?

    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Oct 11, 2004, in forum: Python
    Replies:
    3
    Views:
    293
    Alex Martelli
    Oct 12, 2004
  2. Replies:
    5
    Views:
    556
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    578
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    726
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    232
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page