From Dict to Classes yes or no and how

Andreas Waldenburger usenot at geekmail.INVALID
Tue Jun 22 18:26:37 EDT 2010


On Tue, 22 Jun 2010 13:56:43 +0200 (CEST) "Jerry Rocteur"
<macosx at rocteur.cc> wrote:

> As part of learning Python, I'm also learning OOP! That is why I want
> to know if this is doable using classes.

Everything[1] is doable using classes. The question is: Do you *need* to
do it with classes? If your problem is best described as "mappings from
keys to values", then you have already implemented a fine solution
with dicts.

Think of "objects" as "things with behavior". Basically, if you have
many top-level data items (such as dicts, lists, whatever), and many
functions operating on these, grouping these data items with the
appropriate functions within classes might clean up your code
conceptually (It might! I didn't say it will!).

If you can categorize these data/function groups into a hierarchical
structure of ever more specialized functionality, then classes and
inheritance are pretty much your friends by default.

So the takeaway is this: Will you (and possibly others) be able to
understand my code better by organizing it in this-or-that structure?
If so, use this-or-that structure, if not, then don't.


And to even more evade answering your specific question: If you have
the time, go and rewrite your code with classes. That'll teach you more
than theorizing about *maybe doing* it here.

/W

[1] For sufficiently small values of "everything".

-- 
INVALID? DE!




More information about the Python-list mailing list