Skip to content

Commit 2ef5d4d

Browse files
committed
Inheritance
1 parent f7005ab commit 2ef5d4d

7 files changed

+330
-0
lines changed

Inheritance/inheritance-1.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
# inheritance-1.py
4+
5+
# The code below shows how a class can inherit from another class.
6+
# We have two classes, `Date` and `Time`. Here `Time` inherits from
7+
# `Date`.
8+
9+
# Any class inheriting from another class (also called a Parent class)
10+
# inherits the methods and attributes from the Parent class.
11+
12+
# Hence, any instances created from the class `Time` can access
13+
# the methods defined in the parent class `Date`.
14+
15+
16+
class Date(object):
17+
def get_date(self):
18+
print("2016-05-14")
19+
20+
21+
class Time(Date):
22+
def get_time(self):
23+
print("07:00:00")
24+
25+
26+
# Creating an instance from `Date`
27+
dt = Date()
28+
dt.get_date() # Accesing the `get_date()` method of `Date`
29+
print("--------")
30+
31+
# Creating an instance from `Time`.
32+
tm = Time()
33+
tm.get_time() # Accessing the `get_time()` method from `Time`.
34+
# Accessing the `get_date() which is defined in the parent class `Date`.
35+
tm.get_date()
36+
37+
'''
38+
O/P-
39+
2016-05-14
40+
--------
41+
07:00:00
42+
2016-05-14
43+
'''

Inheritance/inheritance-2.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
# inheritance-2.py
4+
5+
# The code below shows another example of inheritance
6+
# Dog and Cat are two classes which inherits from Animal.
7+
# This an instance created from Dog or Cat can access the methods
8+
# in the Animal class, ie.. eat().
9+
10+
# The instance of 'Dog' can access the methods of the Dog class
11+
# and it's parent class 'Animal'.
12+
13+
# The instance of 'Cat' can access the methods of the Cat class
14+
# and it's parent class 'Animal'.
15+
16+
# But the instance created from 'Cat' cannot access the attributes
17+
# within the 'Dog' class, and vice versa.
18+
19+
20+
class Animal(object):
21+
def __init__(self, name):
22+
self.name = name
23+
24+
def eat(self, food):
25+
print("%s is eating/drinking %s" % (self.name, food))
26+
27+
28+
class Dog(Animal):
29+
def fetch(self, thing):
30+
print("%s goes after the %s" % (self.name, thing))
31+
32+
33+
class Cat(Animal):
34+
def swatstring(self):
35+
print("%s shred the string!" % self.name)
36+
37+
38+
dog = Dog("Tuffy")
39+
cat = Cat("Mona")
40+
41+
dog.fetch("paper")
42+
dog.eat("bone")
43+
print("--------")
44+
cat.eat("milk")
45+
cat.swatstring()
46+
47+
'''
48+
O/P-
49+
Tuffy goes after the paper
50+
Tuffy is eating/drinking bone
51+
--------
52+
Mona is eating/drinking milk
53+
Mona shred the string!
54+
'''
55+
56+
# The below methods would fail, since the instances doesn't have
57+
# have access to the other class.
58+
59+
cat.fetch("frizbee")
60+
dog.swatstring()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
# inheriting-init-constructor-1.py
4+
5+
# This is a normal inheritance example from which we build
6+
# the next example. Make sure to read and understand the
7+
8+
9+
10+
class Animal(object):
11+
def __init__(self, name, thing):
12+
self.name = name
13+
self.thing = thing
14+
15+
16+
class Dog(Animal):
17+
def fetch(self):
18+
print("%s goes after the %s" % (self.name, self.thing))
19+
20+
21+
dog = Dog("Tuffy","Ball")
22+
print("The dog's name is %s and he plays %s" % (dog.name, dog.thing))
23+
dog.fetch()
24+
'''
25+
O/P-
26+
The dog's name is Tuffy and he plays Ball
27+
Tuffy goes after the Ball
28+
'''
29+
30+
#ANOTHER WAY TO ACCESS
31+
class Animal(object):
32+
def __init__(self, name):
33+
self.name = name
34+
35+
36+
class Dog(Animal):
37+
def fetch(self, thing):
38+
print("%s goes after the %s" % (self.name, thing))
39+
40+
41+
d = Dog("TUFFY")
42+
print("The dog's name is", d.name)
43+
d.fetch("Ball")
44+
'''
45+
O/P-
46+
The dog's name is TUFFY
47+
TUFFY goes after the Ball
48+
'''

Inheritance/multiple-inheritance-1.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
# multiple-inheritance-1.py
4+
5+
# Python supports multiple inheritance and uses a depth-first order
6+
# when searching for methods.
7+
# This search pattern is call MRO (Method Resolution Order)
8+
9+
# This is the first example, which shows the lookup of a common
10+
# function named 'dothis()', which we'll continue in other examples.
11+
12+
# As per the MRO output, it starts in class D, then B, A, and lastly C.
13+
14+
# Both A and C contains 'dothis()'. Let's trace how the lookup happens.
15+
16+
# As per the MRO output, it starts in class D, then B, A, and lastly C.
17+
18+
# class `A` defines 'dothis()' and the search ends there. It doesn't go to C.
19+
20+
# The MRO will show the full resolution path even if the full path is
21+
# not traversed.
22+
23+
# The method lookup flow in this case is : D -> B -> A -> C
24+
25+
26+
class A(object):
27+
def dothis(self):
28+
print("doing this in A")
29+
30+
31+
class B(A):
32+
pass
33+
34+
35+
class C(object):
36+
def dothis(self):
37+
print("doing this in C")
38+
39+
40+
class D(B, C):
41+
pass
42+
43+
44+
d_instance = D()
45+
d_instance.dothis() # <== This should print from class A.
46+
47+
print("\nPrint the Method Resolution Order")
48+
print(D.mro())
49+
50+
'''
51+
O/P-
52+
doing this in A
53+
54+
Print the Method Resolution Order
55+
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
56+
'''

Inheritance/multiple-inheritance-2.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
# multiple-inheritance-2.py
4+
5+
# Python supports multiple inheritance
6+
7+
# It uses a depth-first order when searching for methods.
8+
# This search pattern is call MRO (Method Resolution Order)
9+
10+
# This is a second example, which shows the lookup of 'dothis()'.
11+
# Both A and C contains 'dothis()'. Let's trace how the lookup happens.
12+
13+
# As per the MRO output using depth-first search,
14+
# it starts in class D, then B, A, and lastly C.
15+
16+
# Here we're looking for 'dothis()' which is defined in class `C`.
17+
# The lookup goes from D -> B -> A -> C.
18+
19+
# Since class `A` doesn't have `dothis()`, the lookup goes back to class `C`
20+
# and finds it there.
21+
22+
23+
class A(object):
24+
def dothat(self):
25+
print("Doing this in A")
26+
27+
28+
class B(A):
29+
pass
30+
31+
32+
class C(object):
33+
def dothis(self):
34+
print("\nDoing this in C")
35+
36+
37+
class D(B, C):
38+
"""Multiple Inheritance,
39+
D inheriting from both B and C"""
40+
41+
pass
42+
43+
44+
d_instance = D()
45+
46+
d_instance.dothis()
47+
48+
print("\nPrint the Method Resolution Order")
49+
print(D.mro())
50+
51+
'''
52+
O/P-
53+
Doing this in C
54+
55+
Print the Method Resolution Order
56+
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
57+
'''

Inheritance/multiple-inheritance-3.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
3+
# multiple-inheritance-3.py
4+
5+
# Python supports multiple inheritance
6+
# and uses a depth-first order when searching for methods.
7+
# This search pattern is call MRO (Method Resolution Order)
8+
9+
# Example for "Diamond Shape" inheritance
10+
# Lookup can get complicated when multiple classes inherit
11+
# from multiple parent classes.
12+
13+
# In order to avoid ambiguity while doing a lookup for a method
14+
# in various classes, from Python 2.3, the MRO lookup order has an
15+
# additional feature.
16+
17+
# It still does a depth-first lookup, but if the occurrence of a class
18+
# happens multiple times in the MRO path, it removes the initial occurrence
19+
# and keeps the latter.
20+
21+
# In the example below, class `D` inherits from `B` and `C`.
22+
# And both `B` and `C` inherits from `A`.
23+
# Both `A` and `C` has the method `dothis()`.
24+
25+
# We instantiate `D` and requests the 'dothis()' method.
26+
# By default, the lookup should go D -> B -> A -> C -> A.
27+
# But from Python 2.3, in order to reduce the lookup time,
28+
# the MRO skips the classes which occur multiple times in the path.
29+
30+
# Hence the lookup will be D -> B -> C -> A.
31+
32+
33+
class A(object):
34+
def dothis(self):
35+
print("doing this in A")
36+
37+
38+
class B(A):
39+
pass
40+
41+
42+
class C(A):
43+
def dothis(self):
44+
print("doing this in C")
45+
46+
47+
class D(B, C):
48+
pass
49+
50+
51+
d_instance = D()
52+
d_instance.dothis()
53+
54+
print("\nPrint the Method Resolution Order")
55+
print(D.mro())
56+
57+
'''
58+
O/P-
59+
doing this in C
60+
61+
Print the Method Resolution Order
62+
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
63+
'''

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,8 @@ def __init__(self):
121121
------------
122122
#### 05. Inheritance
123123

124+
With inheritance one class can derive the properties of another class.
125+
ex- Man Inheriting features from his father
124126

127+
[Detailed Explanation](Inheritance)
125128
------------

0 commit comments

Comments
 (0)