Heterogeneous lists

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

  1. Gordon Airporte a écrit :
    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...
    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. Advertisements

  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. Advertisements

  3. Bruno Desthuilliers

    Jarek Zgoda Guest

    Bruno Desthuilliers napisa³(a):
    I love my lists of classes. I know, I'll go to hell for that.
     
    Jarek Zgoda, Aug 7, 2007
    #3
  4. Bruno Desthuilliers

    Tony Guest

    And I love my shelved lists of classes..

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

    faulkner Guest

    how else would you implement an n-ary tree? eg, AST, CST, minimax, GP.
     
    faulkner, Aug 7, 2007
    #5
  6. I do it all the time - I only use tuples when I _have_ to.
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.