Heterogeneous lists

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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

Gordon Airporte

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?
 
J

Jarek Zgoda

Bruno Desthuilliers napisa³(a):
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...

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

faulkner

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

Hendrik van Rooyen

Gordon Airporte said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top