Heterogeneous lists

Discussion in 'Python' started by Bruno Desthuilliers, Aug 6, 2007.

  1. Gordon Airporte a écrit :
    > This is one of those nice, permissive Python features but I was
    > wondering how often people actually use lists holding several different
    > types of objects.


    Depends on the definition of 'type'. I often have instances of different
    - possibly unrelated - classes in a same list. Fact is that these
    instances usually share a common (implied) interface, but, well,
    sometimes they don't...

    > It looks like whenever I need to group different objects I create a
    > class, if only so I can use more meaningful names than '[2]' for the items.


    You may not know, but Python has a builtin dict (ie : hashtable) type.
    It's very handy when you just want to "group different objects" while
    still using meaningful names.
     
    Bruno Desthuilliers, Aug 6, 2007
    #1
    1. Advertising

  2. This is one of those nice, permissive Python features but I was
    wondering how often people actually use lists holding several different
    types of objects.
    It looks like whenever I need to group different objects I create a
    class, if only so I can use more meaningful names than '[2]' for the items.
    How often do these show up in your code?
    Is this simply the upshot of the underlying arrays holding only object
    references of some sort?
     
    Gordon Airporte, Aug 7, 2007
    #2
    1. Advertising

  3. Bruno Desthuilliers

    Jarek Zgoda Guest

    Bruno Desthuilliers napisa³(a):

    > Gordon Airporte a écrit :
    >> This is one of those nice, permissive Python features but I was
    >> wondering how often people actually use lists holding several
    >> different types of objects.

    >
    > Depends on the definition of 'type'. I often have instances of different
    > - possibly unrelated - classes in a same list. Fact is that these
    > instances usually share a common (implied) interface, but, well,
    > sometimes they don't...


    I love my lists of classes. I know, I'll go to hell for that.

    --
    Jarek Zgoda
    http://jpa.berlios.de/
     
    Jarek Zgoda, Aug 7, 2007
    #3
  4. Bruno Desthuilliers

    Tony Guest

    On Aug 7, 8:53 pm, Jarek Zgoda <> wrote:
    ..
    >
    > I love my lists of classes. I know, I'll go to hell for that.
    >
    > --
    > Jarek Zgodahttp://jpa.berlios.de/


    And I love my shelved lists of classes..

    Tony
     
    Tony, Aug 7, 2007
    #4
  5. Bruno Desthuilliers

    faulkner Guest

    On Aug 7, 2:53 pm, Gordon Airporte <> wrote:
    > This is one of those nice, permissive Python features but I was
    > wondering how often people actually use lists holding several different
    > types of objects.
    > It looks like whenever I need to group different objects I create a
    > class, if only so I can use more meaningful names than '[2]' for the items.
    > How often do these show up in your code?
    > Is this simply the upshot of the underlying arrays holding only object
    > references of some sort?


    how else would you implement an n-ary tree? eg, AST, CST, minimax, GP.
     
    faulkner, Aug 7, 2007
    #5
  6. "Gordon Airporte" <J,,,,>

    > This is one of those nice, permissive Python features but I was
    > wondering how often people actually use lists holding several different
    > types of objects.


    I do it all the time - I only use tuples when I _have_ to.

    > It looks like whenever I need to group different objects I create a
    > class, if only so I can use more meaningful names than '[2]' for the items.


    You don't have to be so array-minded. You can write things like:

    address = 2
    individual_list[address]

    this is easier to read and understand than:

    individual_list[2]

    and does the same thing.

    I have found that the most useful data structure is a dict,
    or a dict of dicts. - almost an instant database.

    - Hendrik
     
    Hendrik van Rooyen, Aug 8, 2007
    #6
    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. kj
    Replies:
    1
    Views:
    359
    Ashmodai
    Apr 15, 2004
  2. Mike
    Replies:
    3
    Views:
    452
  3. Jason
    Replies:
    0
    Views:
    395
    Jason
    Aug 21, 2003
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    407
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Willi
    Replies:
    7
    Views:
    378
    Öö Tiib
    Mar 19, 2013
Loading...

Share This Page