Object-Oriented Programming
#
Defining ClassesExample: Defining a Fraction
class.
self
is a special parameter which makes reference to the object itself and is always the first parameter. However, it is not supplied as an argument.
#
Static VariablesStatic variables are shared among all instances of the class. For example, if we have anEmployee
class, num_of_employees
can be a static variable which keeps count of the total number of employees.
#
Printing an ObjectTo print an object, we can define a method 'show'.
Otherwise, we need to tell Python how to convert the object into a string by overriding the __str__
method.
#
Overriding Default MethodsJust as how we overrode the default __str__
method, we can override the other default methods such as the +
arithmetic operator.
#
Deep EqualityShallow equality compares the references of two objects. To have deep equality, where we compare the contents of the two objects, we need to override the __eq__
method. Other relational operators such as less than can be achieved by overriding the __le__
method.