Using Classes

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Jun 24 08:20:41 EDT 2010


Mag Gam a écrit :
> I have been using python for about 1 year now and I really like the
> language. Obviously there was a learning curve but I have a programing
> background which made it an easy transition. I picked up some good
> habits such as automatic code indenting :-), and making my programs
> more modular by having functions.
> 
> I know that Python is very OOP friendly, but I could not figure out
> why and when to use Classes in it.

If you have some more or less formal "data type" defined by ie dicts 
with some particular keys in them or tuples with a known structure, and 
a few fonctions working on these dicts or tuples, then you have a 
perfect use case for OO.

For other use case, have a look at the csv module or the various XML / 
SGML / HTML parsers in the stdlib. There's a pretty interesting paper 
from Alex Martelli here:

http://www.aleax.it/Python/os03_template_dp.pdf


> I mostly use it for simple text
> parsing  I suppose when a program gets "complicated" I should start
> using Classes.

Not necessarily. OO is one way to organize code and data, but there are 
other ways that work as well, depending on the problem at hand and how 
your brain is connected.

> Are there any tips or tricks people use to "force" them
> into the OOP mentality? I would like to force myself to learn the
> Python way but so far I could not figure out WHY I would need a class
> for this...

Not going into OO when you don't need it IS actually the "Python way" !-)



More information about the Python-list mailing list