Using Classes

Nathan Rice nathan.alexander.rice at gmail.com
Thu Jun 24 10:27:03 EDT 2010


As others have mentioned when you would like to encapsulate data and
functions together, they're useful.

Also, if you find yourself passing lists/tuples/dictionaries around
frequently, you might benefit from converting them into classes.  This tends
to make the code clearer and more readable, you can refer to things by
attribute name instead of index without unpacking, and you can create custom
processing logic around attributes if need be.

I also have an esoteric use case for classes, when I have a process that is
implemented very slightly differently in several situations.  I break the
elements of a function up into atomic components as methods on a class, then
override the variable parts in subclasses.  I find that this lets me re-use
more code than writing multiple different top-level functions.

On Thu, Jun 24, 2010 at 7:58 AM, Mag Gam <magawake at gmail.com> wrote:

> 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. I mostly use it for simple text
> parsing  I suppose when a program gets "complicated" I should start
> using Classes. 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...
>
>
> TIA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100624/4223bf3b/attachment.html>


More information about the Python-list mailing list