classes and standard modules

B

Brad Tilley

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.

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?
 
J

Jeremy Bowers

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?

Yes.

Without further information it is hard to add anything to the examples
that accompany the various libraries.

A couple people may jump in here to describe classes again; I'm going to
instead suggest just using Python for a while and taking a crack at it
again later when you've used more Python. For some pragmatically-minded
types of people, it is much easier to describe how classes work when we
have something concrete to point at. Later on, post some code you're
wondering about and ask questions (as specific as possible) and we'll be
happy to help.
 
P

phansen

Brad said:
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. :)

-Peter
 
I

Istvan Albert

Brad said:
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?

Theoretically yes, in practice no.

Istvan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top