Python Modules, Packaages, and Objects

Modules , packages, and objects are a way of grouping python code.

Modules
Are just collections of related code organized via the file system.  All functions within a module share a symbol table, and cannot access global variables from your main program.  This measn that 'count' as used in a module will not conflict with your 'count'.

import echo
looks for a file named 'echo' and imports it.  If the file contains any executable code, that code is run (initalization??) .  If it defines any functions those functions are added to your program.  For example, if the file echo.py has a function fred, you have just added the function echo.fred to your program.

from echo import fred
looks for the function fred in the file echo.py, adding the function fred to your code.

from echo import *
looks for the file echo.py, and adds the functions fred (not echo.fred) to your code.

Packages
Are just file system directories containing related modules.

from sound import echo
If the file sound.py exists, imports the function echo.
If the file sound/echo.py exists,  you now have functions like sound.echo.fred()

from sound.extra.stuff import *
If the file sound/extra/stuff.py exists, imports all functions within.  You now have functions like fred()
If the directory sound/extra/stuff exists, does an import of ALL files in that directory, creating things like sound.extra.stuff.fred()

Classes
All members of a class are public and virtual.  As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to ``break into the definition.'

class c:
        i = 0
        def __init__(self):
                print self.i
        def set(self, x):
                self.i = x
Many classes like to create objects in a known initial state. Therefore a class may define a special method named __init__(), like this:
     def __init__(self):
          self.empty()

Here's an interesting class

class MyClass:
         "A simple example class"
          i = 12345
          def f(x):
              return 'hello world'
x = MyClass()
xf = x.f
      while 1:
          print x.f()
          print xf()
Classes can have attributes added to them after the class is declared.  Can you think of another language that allows this?  What would you use it for?  Is this cool, or just a new way to add bugs to a program?
class Employee:
          pass

      john = Employee() # Create an empty employee record

      # Fill the fields of the record
      john.name = 'John Doe'
      john.dept = 'computer lab'
      john.salary = 1000

Inheritance

Python supports multiple inheritance or single inheritance.  The syntax is

class DerivedClassName(Base1, Base2, Base3):
          <statement-1>
          .
          .
          .
          <statement-N>