Everything in Python is an 'object'.
Defining custom types of objects is easy:
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def fire(self): self.salary = 0
selfparameter, through which the object is passed.
__init__is the 'constructor'
joe = Employee("Joe", 100000) joe.name
Let's fire Joe.
Types can be based on other types by inheritance:
class Boss(Employee): def __init__(self, name, salary, supervises): super(Boss, self).__init__(name, salary) self.supervises = supervises def fire(self): for s in self.supervises: s.fire() super(Boss, self).fire()
joe = Employee("Joe", 100000) jack = Employee("Jack", 100000) mike = Boss("Mike", 150000, [joe, jack]) mike.salary
Now what happens to Joe's salary if Mike gets fired?