classes and standard modules

phansen peter at
Fri Sep 17 14:36:48 CEST 2004

Brad Tilley wrote:
> I don't understand classes very well... maybe some day. Is it just me or 
> are they supposed to be difficult to understand? They make my head hurt.

They're not really that hard.  Classes are nothing more than
_templates_ for blobs of data with associated functions.  When
you instantiate a class (i.e. create an instance) you just get
one of those blobs of data, matching the pattern defined by the
template.  You can call those associated functions and they
already know how to work on the data in the blob.

If you have a C background, think of classes as being structs with
a bunch of functions that are designed to work with those structs.

> Anyway, because I don't understand classes well, I avoid using them. 
> However, many modules in the standard library are implemented as 
> classes: sgmllib, HTMLParser, etc. Is it possible to use these modules 
> without getting into OO programming and inheritance and all the other 
> lofty, theoretical CS concepts that small script writers, like me, don't 
> really need?

Well, for any of those, you just create one of the blobs of data,
say like "server = smtplib.SMTP('myhost')", and now you have a blob
with the name "server".  You want to call one of those functions?
Instead of passing the data in the argument list as you do with
regular functions, you use the syntactic sugar form that looks like
this: "server.sendmail(...)".

This description needs you to know about blobs of data and functions,
neither of which are very lofty or theoretical, I think.  :)


More information about the Python-list mailing list