External Exam Download Resources Web Applications Games Recycle Bin

Multiple Classes

inner classes.py

#note - it is recommended NOT to use inner (aka
#nested) classes, as the nesting does not imply
#any particular relationship between inner and
#outer classes.  With that said, here is example:

class Human:
  num_of_humans = 0

  def __init__(self, lastname):
    Human.num_of_humans = Human.num_of_humans + 1
    self.lastname = lastname
    self.head = self.Head()
    self.brain = self.Brain()

  #these are "inner" or "nested" classes:
  class Head:
    #this isn't great design - referring to outer class:
    def talk(self, outer_instance):
      print(outer_instance.lastname + " is talking...")
  
  class Brain:
    def think(self):
      print("a brain is thinking...")

#---------MAIN---------

caroline = Human("Chisholm")

caroline.head.talk(caroline)
caroline.brain.think()

tom = Human("Cruise")

print(Human.num_of_humans, "humans created so far.")


inheritance.py

class Alive:
  number = 0
  def __init__(self, name):
    self.name = name
    Alive.number = Alive.number + 1

#Human "inherits" the properties of the Alive class,
#and in this case we will access the instance
#variable self.name which was defined in Alive class:
class Human(Alive):
  def talk(self):
    print(self.name + " now speaking.")

#each Dog also inherits a self.name (Alive class)
#so no need to redefine here:
class Dog(Alive):
  #no self - this is a class method:
  def woof():
    return "bow wow"


#---------MAIN---------
caroline = Human("Chisholm")
tom = Human("Cruise")
santas = Dog("little-helper")

#instance variable, caroline.name and santas.name:
print(caroline.name)
print(santas.name)

#instance method, tom.talk():
tom.talk()

#class method, Dog.woof():
print(Dog.woof())
#note - fido.woof() would not work here.

#class variable, Alive.number:
print(Alive.number)