[Tutor] Difference between a class & module? (original) (raw)
Chad Crabtree flaxeater at yahoo.com
Sat Jul 3 12:44:19 EDT 2004
- Previous message: [Tutor] Difference between a class & module?
- Next message: [Tutor] Difference between a class & module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--- 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
- Previous message: [Tutor] Difference between a class & module?
- Next message: [Tutor] Difference between a class & module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]