6.8 Modules and classesModules provide a convenient way to group functions and variables together. Pyxplot’s default functions are grouped into modules such as os and random. New modules can be created by calling the module object, which is a synonym for types.module. Once created, a module is like a dictionary, except that its elements can be accessed both as module[item] and more commonly as module.item. For example: pyxplot> myFuncs = module() Modules can also serve as class prototypes. If a module is called like a function, the return value is an instance of the module: pyxplot> oldMan = module() The module instance inherits all of the functions and variables of its parent object, but may also contain its own additional functions and variables, some of which may supersede those in the parent object if they have the same name. When functions or subroutines of a module instance are called, the special variable self is defined to equal the module instance object. This allows the function to store private data in the module instance, or to call other methods on the instance. pyxplot> animal = module() As this example demonstrates, it is also possible to hierarchically instantiate modules: tiddles is an instance of cat, which is itself an instance of animal.
|