[Tutor] Difference between a class & module?

Chad Crabtree flaxeater at yahoo.com
Sat Jul 3 12:44:19 EDT 2004


--- Alan Gauld <alan.gauld at blueyonder.co.uk> wrote:
> 
> > I thought that some frequently used "modules" were built-in and
> that
> > string was one of them... not so.
> 
> Some modules are indeed built in. And the string class is also
> built in, but that's not the same as the string module!
> 
> > 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.
> 
> No, a module is, in its most general form, any kind of reusable
> code
> block. Thus a function is a type of module as is a class. However
> in Python, module has the more specific meaning of a file
> containing
> functions, data, (and classes which are a special type of data!).
> Modules (or more specifically their names or the names inside them)
> are imported into Python programs. Classes by contrast are
> instantiated not imported.
> 
> For a more complete explanation try my tutorial, the topic on
> "Modules and Functions" and the OOP topic for classes/objects.
> 
> Alan G
> Author of the Learn to Program web tutor
> 
> http://www.freenetpages.co.uk/hp/alan.gauld/tutor2
> 
I just wanted to add a little tid bit incase readers do not know of
this.   I use this all the time when I'm learning about things.

>>> dir("")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center',
'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find',
'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace',
'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__',
'__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__',
'__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__',
'__setitem__', '__setslice__', '__str__', 'append', 'count',
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> 

This allows me to know what built-in objects can do.  Sometimes I go
through the list and find the type so that I can know if it's a
function or constant like so

for x in dir([]):
  print type(x)

then if you need to know the use of a built-in object just type
help(''.split) and read on.  Many know this already.  But it needs to
be repeated occasionaly I think.  :)




		
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 



More information about the Tutor mailing list