[Tutor] dicts&lists vs objects

Smith, Jeff jsmith at medplus.com
Thu May 12 15:08:20 CEST 2005


Those are good observations and I think answers part of the question.  I
think the other part is that even in OO code, how do you know what to
make an object and what to just store in an existing data type like a
list or dictionary.  

Personally, I use the "if it walks like a duck" rule.  In other words,
if you are storing an ordered list of items which are logically to each
other, use a list.  If you are storing an un-ordered list of logically
related items which you think of referencing by some other list, use a
dictionary.  Anything else is an object.

Another way to look at it is that anytime you have two or more lists of
things which you are trying to keep connected by some arbitrary variable
to keep track of the index, you should be using an object.  This happens
frequently in scientific code:

elements = ['al', 'ar', 'o']
boiling_points = [500, 400, 150]
element = 0

for i in xrange(1,len(elements)):
	stuff with elements[i] and boiling points[i]

could be a dictionary:

boiling_points = ['al': 500, 'ar': 400, 'o': 150]

for (element, bp) in elements.iteritems():
	stuff with element and bp

But add one more property and you need a class

class Element(object):
	def __init__(self, name, boiling_point, melting_point):
		you get the picture

Jeff

Disclaimer:  I'm new to Python and wrote this on the fly, so if there
are any coding errors from a Python standpoint, then this isn't Python
but pseudo code which happens to resemble Python :-)

Disclaimer 2: I'm not a materials scientist so don't let my numbers
cause problems for those of you who are! 






-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Andrei
Sent: Thursday, May 12, 2005 4:59 AM
To: tutor at python.org
Subject: Re: [Tutor] dicts&lists vs objects


Chris Somerlot <csomerlot <at> gmail.com> writes:

> is a premutation of another, and I'm having trouble following the code

> I've written. Is there a golden rule to knowing when to use objects 
> instead of dictionaries
and lists?

It all depends on the programmer, the project, etc. I'd say that the
fact that you're having trouble following your code is an indication
that it might be time to drop the procedural/inline approach for
something more structured. Not that it's impossible to write
unmaintainable OOP code by the way, far from it :). E.g. imagine having
one huge big Application object with everything that was a function
having become a method of this monolithic object. Not an improvement at
all.

Depending on what/how you write, I'd say that for code over 50 lines
(say 1h of
work) an OO approach at least deserves consideration and will often turn
out to have some benefits (it's possible to mix OO and procedural code).
If a project runs into thousands of lines of code, it virtually always
makes sense to bring OO into the picture.

Yours,

Andrei

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list