Re: List vs tuples

From: Isaac To (kkto_at_csis.hku.hk)
Date: 04/09/04


Date: Fri, 09 Apr 2004 17:46:18 +0800


>>>>> "John" == John Roth <newsgroups@jhrothjr.com> writes:

    John> It's a common question. A list is a data structure that is
    John> intended to be used with homogenous members; that is, with members
    John> of the same type.

I feel you argument very strange. Even that it might be the original
intention, I see nothing in the language that support your argument.

In particular, nothing says that tuples cannot be used (or is troublesome to
use) when members are of the same type. And nothing says that lists cannot
be used (or is troublesome to use) when members are of different types.

    John> You've probably used tuples without realizing it. For example, the
    John> following idiom involves a tuple:

    John> hours, minute, second = getTime()

    John> where

    John> def getTime(.....) #some calc return hour, minute, second

    John> While it looks like return is returning multiple objects, it
    John> actually isn't. It's returning one object: a tuple of three
    John> elements which is then being unpacked on the receiving end.

If getTime() give you a list instead, then [hours, minute, second] =
getTime() would still give you the hours, minute and second variables
assigned.

    John> You could call the same routine this way as well:

    John> result = getTime()

    John> and result would be a tuple of three elements.

And if getTime() give you a list of course result would be a list instead.
Nothing magic.

    John> Another way of thinking about it is that you would use a tuple the
    John> same way you would use a struct in a language like C, if you
    John> didn't want to go to the trouble of creating a full-fledged object
    John> for it.

And you can use a list here as well. Indeed, if you want to emulate C
struct, you probably will use list instead of tuples, since in C, structs
are mutable.

Regards,
Isaac.



Relevant Pages

  • Re: List vs tuples
    ... > John> It's a common question. ... > use) when members are of the same type. ... And nothing says that lists ... A subset of the fields of a struct is not of the same ...
    (comp.lang.python)
  • Re: Looking up dependent values in a list
    ... John wrote: ... apples can be sourced from any location. ... > "Frank Kabel" wrote in message ... >>> values are in lists but once a certain "value" is selected I want ...
    (microsoft.public.excel.programming)
  • Re: Forgive me if this has already been posted...Bob at #7!!!
    ... 81 John Lee Hooker ... 74 James Taylor ... 68 Wilson Pickett ... Fuck lists. ...
    (rec.music.dylan)
  • Re: This is probably the simplest of questions but I am missing th
    ... owners and owners addresses etc. ... from various other drop down lists which provide other relevant info. ... John is perhaps a lot better at this than I am if he has not dropped the ... from each and then add more info which is relevant to a third database. ...
    (microsoft.public.access.forms)
  • Re: List vs tuples
    ... John> tuples can, and more (except be used as keys in dicts, besides ... lists instead. ... struct in C++, ... I think this by itself serves a very clear documentary ...
    (comp.lang.python)