Skip to content

Commit 63e7d2f

Browse files
committed
Added Examples to Inheritance Demonstrations
1 parent 77859e1 commit 63e7d2f

File tree

9 files changed

+314
-16
lines changed

9 files changed

+314
-16
lines changed

Classes-Tutorial.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,21 @@
3737
}
3838
],
3939
"metadata": {
40+
"kernelspec": {
41+
"display_name": "Python 3.10.2 64-bit",
42+
"language": "python",
43+
"name": "python3"
44+
},
4045
"language_info": {
41-
"name": "python"
46+
"name": "python",
47+
"version": "3.10.2"
4248
},
43-
"orig_nbformat": 4
49+
"orig_nbformat": 4,
50+
"vscode": {
51+
"interpreter": {
52+
"hash": "7e1998ff7f8aa20ada591c520b972326324e5ea05489af9e422744c7c09f6dad"
53+
}
54+
}
4455
},
4556
"nbformat": 4,
4657
"nbformat_minor": 2

Dogs.ipynb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 13,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Woof!\n",
13+
"Piper 5 9.5\n",
14+
"Aroooo\n",
15+
"Chloe 5 10\n",
16+
"Aroooo\n"
17+
]
18+
}
19+
],
20+
"source": [
21+
"class Dog:\n",
22+
" def __init__(self, name, age, friendliness):\n",
23+
" self.name = name\n",
24+
" self.age = age\n",
25+
" self.friendliness = friendliness\n",
26+
" \n",
27+
" def likes_walks(self):\n",
28+
" return True\n",
29+
" \n",
30+
" def bark(self):\n",
31+
" return 'Woof!'\n",
32+
"\n",
33+
"# Inheritance - Add Parent Class in Class Definition\n",
34+
"# super() - Parent Class\n",
35+
"class Samyoed(Dog):\n",
36+
" def __init__(self, name, age, friendliness):\n",
37+
" super().__init__(name, age, friendliness) # Calls initialization function in the parent class\n",
38+
"\n",
39+
"\n",
40+
" def bark(self):\n",
41+
" return 'Arf Arf!'\n",
42+
"\n",
43+
"class Poodle(Dog):\n",
44+
" def __init__(self, name, age, friendliness):\n",
45+
" super().__init__(self, name, age, friendliness) # Calls initialization function in the parent class\n",
46+
"\n",
47+
"class GoldenRetriever(Dog):\n",
48+
" def __init__(self, name, age, friendliness):\n",
49+
" super().__init__(name, age, friendliness) # Calls initialization function in the parent class\n",
50+
"\n",
51+
"# poodle = Poodle('Poodini', 5, 10)\n",
52+
"sammy = Samyoed('Sammy', 2, 10)\n",
53+
"# print(sammy.name, sammy.age, sammy.friendliness)\n",
54+
"print(sammy.bark())\n",
55+
"class BlackLab(Dog):\n",
56+
" def __init__(self, name, age, friendliness):\n",
57+
" super().__init__(name, age, friendliness)\n",
58+
"\n",
59+
"class BlackRetriever(BlackLab, GoldenRetriever):\n",
60+
" def __init__(self, name, age, friendliness):\n",
61+
" super().__init__(name, age,friendliness)\n",
62+
" def bark(self):\n",
63+
" return 'Aroooo'\n",
64+
"\n",
65+
"# Create New Objects\n",
66+
"Piper = BlackRetriever('Piper', 5, 9.5)\n",
67+
"Chloe = BlackRetriever('Chloe', 5, 10)\n",
68+
"print(Piper.name, Piper.age, Piper.friendliness) \n",
69+
"print(Piper.bark())\n",
70+
"print(Chloe.name, Chloe.age, Chloe.friendliness)\n",
71+
"print(Chloe.bark())\n",
72+
"\n",
73+
"\n"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": []
82+
}
83+
],
84+
"metadata": {
85+
"kernelspec": {
86+
"display_name": "Python 3.9.12 ('macos')",
87+
"language": "python",
88+
"name": "python3"
89+
},
90+
"language_info": {
91+
"codemirror_mode": {
92+
"name": "ipython",
93+
"version": 3
94+
},
95+
"file_extension": ".py",
96+
"mimetype": "text/x-python",
97+
"name": "python",
98+
"nbconvert_exporter": "python",
99+
"pygments_lexer": "ipython3",
100+
"version": "3.9.12"
101+
},
102+
"orig_nbformat": 4,
103+
"vscode": {
104+
"interpreter": {
105+
"hash": "22608493959378f606de73a65fea1c2a2865a76055a64344b8428e775cc448e8"
106+
}
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 2
111+
}

Inheritance-Ex1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Person:
2+
def __init__(self, fname, lname):
3+
self.firstName = fname
4+
self.lastName = lname
5+
def printname(self):
6+
print(f"My name is: {self.firstName} {self.lastName} ")
7+
8+
person = Person("Obi Wan", "Kenobi")
9+
person.printname()

Inheritance.ipynb

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"cell_type": "markdown",
3232
"metadata": {},
3333
"source": [
34-
"Inheritance is the ability of a Derived (Child) class to take all functionality from a Base (Parent) class.\n",
34+
"Inheritance allows us to create new classes (child class) that inherit all the attributes and methods from another class\n",
3535
"\n",
3636
"Inheritance Syntax:\n",
3737
"class BaseClass:\n",
@@ -40,6 +40,34 @@
4040
" Body of derived class"
4141
]
4242
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"ename": "",
50+
"evalue": "",
51+
"output_type": "error",
52+
"traceback": [
53+
"\u001b[1;31mRunning cells with 'Python 3.10.2 64-bit' requires ipykernel package.\n",
54+
"Run the following command to install 'ipykernel' into the Python environment. \n",
55+
"Command: '/usr/local/bin/python3.10 -m pip install ipykernel -U --user --force-reinstall'"
56+
]
57+
}
58+
],
59+
"source": [
60+
"class Person:\n",
61+
" def __init__(self, fname, lname):\n",
62+
" self.firstName = fname\n",
63+
" self.lastName = lname\n",
64+
" def printname(self):\n",
65+
" print(f\"My name is: {self.firstName} {self.lastName} \")\n",
66+
"\n",
67+
"person = Person(\"Obi Wan\", \"Kenobi\")\n",
68+
"person.printname()"
69+
]
70+
},
4371
{
4472
"cell_type": "code",
4573
"execution_count": null,
@@ -58,10 +86,21 @@
5886
}
5987
],
6088
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 3.9.0 ('tensorflow_m1')",
91+
"language": "python",
92+
"name": "python3"
93+
},
6194
"language_info": {
62-
"name": "python"
95+
"name": "python",
96+
"version": "3.9.0"
6397
},
64-
"orig_nbformat": 4
98+
"orig_nbformat": 4,
99+
"vscode": {
100+
"interpreter": {
101+
"hash": "6cda60cff8d67cf10b4c2a33fafc9942558b0b5167f5378af9b1316837848ff1"
102+
}
103+
}
65104
},
66105
"nbformat": 4,
67106
"nbformat_minor": 2

Method-Overriding.ipynb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# https://www.programiz.com/python-programming/inheritance\n"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"Generally when overriding a base method, we tend to extend the definition rather than simply replace it. The same is being done by calling the method in base class from the one in derived class (calling Polygon.__init__() from __init__() in Triangle).\n",
17+
"\n",
18+
"A better option would be to use the built-in function super(). So, super().__init__(3) is equivalent to Polygon.__init__(self,3) and is preferred. To learn more about the super() function in Python, visit Python super() function.\n",
19+
"\n",
20+
"Two built-in functions isinstance() and issubclass() are used to check inheritances.\n",
21+
"\n",
22+
"The function isinstance() returns True if the object is an instance of the class or other classes derived from it. Each and every class in Python inherits from the base class object."
23+
]
24+
}
25+
],
26+
"metadata": {
27+
"kernelspec": {
28+
"display_name": "Python 3.10.2 64-bit",
29+
"language": "python",
30+
"name": "python3"
31+
},
32+
"language_info": {
33+
"name": "python",
34+
"version": "3.10.2"
35+
},
36+
"orig_nbformat": 4,
37+
"vscode": {
38+
"interpreter": {
39+
"hash": "7e1998ff7f8aa20ada591c520b972326324e5ea05489af9e422744c7c09f6dad"
40+
}
41+
}
42+
},
43+
"nbformat": 4,
44+
"nbformat_minor": 2
45+
}

Polygon.ipynb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@
4242
},
4343
{
4444
"cell_type": "code",
45-
"execution_count": null,
45+
"execution_count": 4,
4646
"metadata": {},
47-
"outputs": [],
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"Side 1 is 3.0\n",
53+
"Side 2 is 5.0\n",
54+
"Side 3 is 4.0\n",
55+
"The area of the triangle is 6.00\n"
56+
]
57+
}
58+
],
4859
"source": [
4960
"# Define a Triangle class that uses the Polygon class you created in the previous exercise.\n",
5061
"class Triangle(Polygon):\n",
@@ -61,10 +72,6 @@
6172
"t.inputSides()\n",
6273
"t.displaySides()\n",
6374
"t.findArea()\n",
64-
"\n",
65-
"\n",
66-
"\n",
67-
"\n",
6875
" \n"
6976
]
7077
}

Super-Function.ipynb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"class Person:\n",
10+
" def __init__(self, fname, lname):\n",
11+
" self.firstName = fname\n",
12+
" self.lastName = lname\n",
13+
" def printname(self):\n",
14+
" print(f\"My name is: {self.firstName} {self.lastName} \")\n",
15+
"\n",
16+
"person = Person(\"Obi Wan\", \"Kenobi\")\n",
17+
"person.printname()\n",
18+
"\n",
19+
"# Example of Super() Function\n",
20+
"class Student(Person):\n",
21+
" def __init__(self, fname, lname):\n",
22+
" super().__init__(fname, lname)"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"Super() Function\n",
30+
"- Python has a super() function that will make the child class inherit all the methods and properties from its parent:"
31+
]
32+
}
33+
],
34+
"metadata": {
35+
"kernelspec": {
36+
"display_name": "Python 3.10.2 64-bit",
37+
"language": "python",
38+
"name": "python3"
39+
},
40+
"language_info": {
41+
"name": "python",
42+
"version": "3.10.2"
43+
},
44+
"orig_nbformat": 4,
45+
"vscode": {
46+
"interpreter": {
47+
"hash": "7e1998ff7f8aa20ada591c520b972326324e5ea05489af9e422744c7c09f6dad"
48+
}
49+
}
50+
},
51+
"nbformat": 4,
52+
"nbformat_minor": 2
53+
}

Traveling-Salesperson.ipynb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,27 @@
9999
"# The following code sets the default search parameters and a heuristic method for finding the first solution: \n",
100100
"search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
101101
"search_parameters.first_solution_strategy = (\n",
102-
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)\n"
102+
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)\n",
103+
"\n"
103104
]
104105
}
105106
],
106107
"metadata": {
108+
"kernelspec": {
109+
"display_name": "Python 3.10.2 64-bit",
110+
"language": "python",
111+
"name": "python3"
112+
},
107113
"language_info": {
108-
"name": "python"
114+
"name": "python",
115+
"version": "3.10.2"
109116
},
110-
"orig_nbformat": 4
117+
"orig_nbformat": 4,
118+
"vscode": {
119+
"interpreter": {
120+
"hash": "7e1998ff7f8aa20ada591c520b972326324e5ea05489af9e422744c7c09f6dad"
121+
}
122+
}
111123
},
112124
"nbformat": 4,
113125
"nbformat_minor": 2

0 commit comments

Comments
 (0)