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
self
parameter, through which the object is passed.__init__
is the 'constructor'joe = Employee("Joe", 100000)
joe.name
joe.salary
Let's fire Joe.
joe.fire()
joe.salary
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
joe.salary
Now what happens to Joe's salary if Mike gets fired?
mike.fire()
joe.salary