- Notifications
You must be signed in to change notification settings - Fork133
Object-Oriented Programming concepts, with Python
License
arvimal/oop_with_python
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Classes
- Instances, Instance methods, Instance attributes
- Class attributes
- Theinit 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
Classes are the building blocks in Object Oriented Programming.
Classes can be seen as blueprints from which you create your Instances.
Attributes or methods specific to a class are called Class attributes
Example:
class MyClass(object): value = 10 def __init__(self): pass
Herevalue
is a class attribute. These are used when certain values need to be set outside a function.
Theinit() constructor is a magic method which gets called when a class is instantiated.
Any attributes set under theinit() constructor will be instantiated at the time of instance creation.
- 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.
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 -> Aa. 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 beD -> 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
Magic methods
About
Object-Oriented Programming concepts, with Python
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.