Object Oriented Programming in Python
- Classes
- Instances, Instance methods, Instance attributes
- Class attributes
- The init constructor
- Inheritance (Inheriting {attributes,methods,constructors etc..})
- Encapsulation
- Polymorphism
- Instance methods
- Multiple Inheritance and method/attribute lookup
- Method Resolution Order (MRO)
- Decorators
- Static methods
- Class methods
NOTES
01. Classes
Classes are the building blocks in Object Oriented Programming.
Classes can be seen as blueprints from which you create your Instances.
02. Instances, Instance methods, Instance attributes
03. Class attributes
Attributes or methods specific to a class are called Class attributes
Example:
class MyClass(object):
value = 10
def __init__(self):
pass
Here value
is a class attribute. These are used when certain values need to be set outside a function.
04. The init constructor
The init() constructor is a magic method which gets called when a class is instantiated.
Any attributes set under the init() constructor will be instantiated at the time of instance creation.
05. Inheritance (Inheriting {attributes,methods,constructors etc..})
06. Encapsulation
07. Polymorphism
08. Instance methods
09. Multiple Inheritance and method/attribute lookup
- Any class can inherit from other classes.
- Any python class can inherit from multiple classes at the same time.
- The class that inherits another class is called the Base/Child class.
- The class being inherited by the Child class is called the Parent class.
- The child class inherits any methods and attributes defined in the parent classes.
- Python uses a depth-first method resolution order (MRO) to fetch methods.
- When two classes inherits from the same class, from Python2.3 onwards, the MRO omits the first occurrence of the class.
- This new MRO lookup method applies from Python2.3, and is for the new-style classes. NOTE: New style classes inherits from the 'object' parent class.
10. Method Resolution Order (MRO)
-
Python has a method lookup order, called
MRO
(Method Resolution Order) -
The MRO path can be printed to stdout using
print <class-name>.mro()
-
Python, by default, uses a depth-first lookup path for MRO.
-
ie.. Imagine you have four classes, A, B, C, D.
- You instance is created from
D
. D
inherits fromB
andC
B
inherits fromA
.- Both
C
andA
has a method with the same name. - Since python follows a depth-first MRO, the method is called from
A
- You instance is created from
REFERENCE: Check the code examples in:
- 14-multiple-inheritance-1.py
- 15-multiple-inheritance-2.py
In some cases, the inheritance can get quite cumbersome when multiple classes inherit from the same classes, in multiple levels.
NOTE : From Python2.3, the MRO has changed slightly in order to speed up the method lookups.
The MRO lookup now skips/omits the initial occurrences of classes which occurs multiple time in the lookup path.
- Example:
- Four classes, A, B, C, D.
- D inherits from both B and C
- B inherits from A
- C inherits from A
- Both C and A contains a similar named method.
- Your instance in created from class D.
- You try a lookup for the method which is named both in A and C.
- The usual lookup should be D -> B -> A -> C -> A a. Hence since the method exists in A and C, it should return from A. b. But since the class A is occurring twice and due to the new MRO method, the lookup will be D -> B -> C -> A.
- The lookup should return the method from class C, rather than A.
REFERENCE: Check the code example in:
- 16-multiple-inheritance-3.py
11. Decorators
12. Static methods
13. Class methods
14. Magic methods
Magic methods