[Tutor] Re: Difference between a class & module?
Andrei
project5 at redrival.net
Fri Jul 2 15:38:37 EDT 2004
Derek Pienaar wrote on Fri, 2 Jul 2004 21:04:36 +0200:
> This question came up when I saw an example in "a byte of python"
> tutorial, where the join method was used, but (as I saw it) without
> importing the string module (as I understood it).
That string module is such an everlasting source of confusion :).
> I thought that some frequently used "modules" were built-in and that
> string was one of them... not so.
A module is not built-in. A module may be part of the standard library, but
it's by definition external (as in: you don't by definition have access to
it, unlike built-ins which are always accessible - unless you overwrite
them of course).
> I understand what a class does ... among other things, it mainly defines
> or is a template for an object. I understand the concept of objects &
> instances but thought that a module was exactly the same as a class.
Nope. The string module is from before strings had methods (IOW, strings
weren't objects) - at least this is what I assume, since I wasn't using
Python at the time.
If you want to work with raw data (as opposed to objects which combine data
and functions), you'll have to define functions which take that data as
parameter, do something with it and return a result. So the string module
contains a few constants and a lot of functions which can be applied to
strings.
When strings became objects, they got methods for doing stuff that was done
before using the functions in the strings module.
OLD way:
import strings
splitstring = strings.split(mystring)
NEW way (split method is provided by a string object):
splitstring = mystring.split()
> To my surprise, I heard it not to be so. What then is a module? Please
> try and keep the explanation plain and simple if possible :-)
Modules are simply collections of code (can be classes, constants,
functions, whatever): every Python program is also automatically a module.
They're most certainly not classes. You can e.g. not subclass a module to
add more functionality.
--
Yours,
Andrei
=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list