[Tutor] Difference between a class & module?
Gregor Lingl
glingl at aon.at
Fri Jul 2 15:24:23 EDT 2004
Derek Pienaar schrieb:
>Halo Guys.
>
>I need to clear up some confusion regarding a class and a module.
>
>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).
>
>I thought that some frequently used "modules" were built-in and that
>string was one of them... not so.
>
>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.
>
>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 :-)
>
>
>
A module simply is a file, which contains Python statements.
Normally among these there are def statements - so execution of
the module defines some functions - and class statements - so
execution of the module defines smo classes. A module may also
contain names (variables) and other directly executable
Python statements.
names (of functions, classes, variables) can be imported
with the import statement:
import <module>
or
from module import <name1, name2 ...>
In the latter case you can use the names directly, in the first one you
have to qualify them with the module name via dot-notation
module.name
May be your confusion araouse becaue there was a heavily used
module string which has become less often used since strings
are built in objects in Python which have their own methods.
Old fashioned way:
import string
s = string.upper(s)
Modern way:
# no import necessary
s = s.upper()
Hope this helps. (Certainly additional explanations
will follow ;-) )
Regards, Gregor
More information about the Tutor
mailing list