Newbie with some doubts.

Mike Meyer mwm at mired.org
Sat Jan 7 18:47:20 EST 2006


Claudio Grondi <claudio.grondi at freenet.de> writes:
> Edgar A. Rodriguez wrote:
>> Hi everybody,
>> Im newbie to Python (I found it three weeks ago) , in fact Im newbie
>> to
>> programming. I'm being reading and training with the language, but I
>> still wondering about what Classes are used to. Could you please give
>> me some examples??

As far as I'm concerned, the definitive work in this area is Meyer's
"Object Oriented Software Construction". He covers pretty much all the
uses of OO language features, using a language that was designed
specifically to support those uses. Be warned that after reading it,
you're liable to come back to Python and wonder "Why doesn't Python do
X".

For a good overview of a most programming paradigms, check out
"Concepts, Techniques, and Models of Computer Programming" by Van Roy
and Haridi. He covers OO programming, but not in as much depth as
Meyer. But you'll see how it fits in with other paradigms.

> There is actually no real need for usage of Classes.

Well, there's actually no real need for usage of namespaces,
functions, modules, and a host of other things one finds in modern
programming languages, because all those languages are turing
complete. But using those features makes it easier to write, read, and
maintain programs - that's why the languages have them.

If your methods never use the self variable, or you never create more
than one instance of a class, then there's a good chance your program
would be easier to read if it didn't use classes. On the other hand,
if you need multiple instances of some object, then an OO version of
the program is often easier to read than the alternatives.

> I for myself try to avoid classes where I can, especially
> inheritance, because I consider the latter in most cases evil.

Well, if you can do the job as well without classes or inheritance (or
multiple inheritance, which some language designers dislike enough
that they leave it out), then I'd say do it that way, because those
are powerful mechanisms. More powerful mechanisms generally take more
work to understand, so if a simpler mechanism - which should be easier
to understand - can do the job, you should prefer it.

On the other hand, maintaining and extending the code is often part of
the job. Using classes and inheritance make this part of the job much
easier. For instance, I need to pickle builtin types that the provided
pickle modules can't handle. If they hadn't used classes, or I chose
not to use inheritance, I'd wind up working on that module - or a copy
of it - and patching it to do what I want. Instead, I'm inheriting
from the pickle modules classes, and extending them. This won't be a
lot less work now. But when I upgrade my python installation, that
changes. If I used "cut-n-paste" extension, my choices would be to use
my version of the module, thus miss out on any bug fixes or
enhancements that are in the new version of the module. Or I can try
and port my patches into the new version, which could well be a lot of
work. Since the pickle module uses classes and I inherited from them,
upgrading Python means I automatically get any fixes and enhancements
to that module. Since I'm only using the public interface, things
should just simply work, with no effort on my part. They may not - so
I need to rerun my tests. But I should do that after upgrading Python,
even if I keep using the old module.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list