>>> class FirstClass:                # Define a class object.
...     def setdata(self, value):    # Define class methods.
...         self.data = value        # self is the instance.
...     def display(self):
...         print self.data          # self.data: per instance



>>> x = FirstClass()                 # Make two instances.
>>> y = FirstClass()                 # Each is a new namespace.



>>> x.setdata("King Arthur")     # Call methods: self is x
>>> y.setdata(3.14159)           # Runs: FirstClass.setdata(y, 3.14159)



>>> x.display()                      # self.data differs in each.
King Arthur
>>> y.display()
3.14159



>>> x.data = "New value"             # Can get/set attributes 
>>> x.display()                      # outside the class too.
New value

>>> x.anothername = "spam"           # Can set new attributes here too. 



>>> class SecondClass(FirstClass):                    # Inherits setdata
...     def display(self):                            # Changes display 
...         print 'Current value = "%s"' % self.data



>>> z = SecondClass()
>>> z.setdata(42)             # setdata found in FirstClass
>>> z.display()               # finds overridden method in SecondClass.
Current value = "42"



>>> x.display()       # x is still a FirstClass instance (old message).
New value



from modulename import FirstClass           # Copy name into my scope.
class SecondClass(FirstClass):              # Use class name directly.
    def display(self): ...

import modulename                           # Access the whole module.
class SecondClass(modulename.FirstClass):   # Qualify to reference
    def display(self): ...



# file food.py

var = 1         # food.var
def func():     # food.func
    ...
class spam:     # food.spam
    ...
class ham:      # food.ham
    ...
class eggs:     # food.eggs
    ...



# person.py

class person:
    ...



import person                  # Import module
x = person.person()            # class within module.

from person import person      # Get class from module.
x = person()                   # Use class name.



>>> class ThirdClass(SecondClass):                # is-a SecondClass
...     def __init__(self, value):                # On "ThirdClass(value)"
...         self.data = value
...     def __add__(self, other):                 # On "self + other"
...         return ThirdClass(self.data + other)
...     def __mul__(self, other):
...         self.data = self.data * other         # On "self * other" 

>>> a = ThirdClass("abc")       # New __init__ called
>>> a.display()                 # Inherited method
Current value = "abc"

>>> b = a + 'xyz'               # New __add__: makes a new instance
>>> b.display()
Current value = "abcxyz"

>>> a * 3                       # New __mul__: changes instance in-place
>>> a.display()
Current value = "abcabcabc"



>>> class rec: pass                # empty namespace object

>>> rec.name = 'Bob'               # just objects with attributes
>>> rec.age  = 40

>>> print rec.name                 # like a struct in C, a record
Bob

>>> x = rec()                      # instances inherit class names
>>> y = rec()

>>> x.name, y.name                 # name is stored on the class only here
('Bob', 'Bob')

>>> x.name = 'Sue'                 # but assignment changes x only
>>> rec.name, x.name, y.name
('Bob', 'Sue', 'Bob')

>>> rec.__dict__.keys()
['age', '__module__', '__doc__', 'name']

>>> x.__dict__.keys()
['name']

>>> y.__dict__.keys()
[]

>>> x.__class__
<class __main__.rec at 0x00BAFF60>

>>> def upperName(self):
        return self.name.upper()      # still needs a self

>>> rec.method = upperName

>>> x.method()                     # run  method to process x 
'SUE'

>>> y.method()                     # same, but  pass y to self
'BOB'

>>> rec.method(x)                  # can call through instance or class
'SUE'

