Skip to content

Commit 565b5c8

Browse files
authored
Add files via upload
1 parent 02cbffb commit 565b5c8

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed

Diff for: Assignment 3.ipynb

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2b4f6107",
6+
"metadata": {},
7+
"source": [
8+
"# Assignment 3"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "abf3c325",
14+
"metadata": {},
15+
"source": [
16+
"## 1. What is the concept of an abstract superclass?"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"id": "6dd86ade",
22+
"metadata": {},
23+
"source": [
24+
"An abstract class/superclass can be considered as a blueprint for other classes. It allows US to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class Whereas an abstract method is a method that has a declaration but does not have an implementation\n"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 1,
30+
"id": "a221b2e8",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"from abc import ABC, abstractmethod\n",
35+
"class Polygon(ABC): # Abstract Class\n",
36+
" @abstractmethod\n",
37+
" def noofsides(self): # Abstract Method\n",
38+
" pass\n",
39+
"class Triangle(Polygon):\n",
40+
" def noofsides(self): # overriding abstract method in child class Triangle\n",
41+
" print(\"I have 3 sides\")\n",
42+
"class Pentagon(Polygon):\n",
43+
" def noofsides(self): # overriding abstract method in child class Pentagon\n",
44+
" print(\"I have 5 sides\")"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"id": "9c49c1f7",
50+
"metadata": {},
51+
"source": [
52+
"## 2. What happens when a class statement's top level contains a basic assignment statement?"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"id": "f31b76ec",
58+
"metadata": {},
59+
"source": [
60+
"When a Class statement's top level contains a basic assignment statement, it is usually treated as a class attribute or class level variable where as assignment statements inside methods are treated as instance attributes or local attributes.\n",
61+
"\n",
62+
"When an instance of a class is created, a single copy of class attributes is maintained and shared to all instances of class where as each instance object maintains its own copy of instance variables."
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 2,
68+
"id": "06a42d88",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"class Human:\n",
73+
" species = 'Homesapiens' # class attribute\n",
74+
" def __init__(self,name,gender):\n",
75+
" self.name = name # instance attributes\n",
76+
" self.gender = gender"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"id": "3d624be3",
82+
"metadata": {},
83+
"source": [
84+
"## 3. Why does a class need to manually call a superclass's __init__ method?"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "ee43a6d6",
90+
"metadata": {},
91+
"source": [
92+
"If a child class has **init** method, then it will not inherit the **init** method of the parent class. In other words the **init** method of the child class overrides the **init** method of the parent class. So, we have to manually call a parent superclass's **init** using super() method"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 4,
98+
"id": "76e72675",
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"name": "stdout",
103+
"output_type": "stream",
104+
"text": [
105+
"{'name': 'sudh', 'age': 26, 'salary': 20000}\n"
106+
]
107+
}
108+
],
109+
"source": [
110+
"class Human:\n",
111+
" def __init__(self,name,age):\n",
112+
" self.name = name\n",
113+
" self.age = age \n",
114+
"class Employee(Human):\n",
115+
" def __init__(self,name,age,salary):\n",
116+
" super().__init__(name,age)\n",
117+
" self.salary = salary\n",
118+
"emp_1 = Employee('sudh',26,20000)\n",
119+
"print(emp_1.__dict__)"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"id": "80bbd736",
125+
"metadata": {},
126+
"source": [
127+
"## 4. How can you augment, instead of completely replacing, an inherited method?"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"id": "8a433a62",
133+
"metadata": {},
134+
"source": [
135+
"super() method can be used to augment, instead of completely replacing, an inherited method."
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 6,
141+
"id": "3440c259",
142+
"metadata": {},
143+
"outputs": [
144+
{
145+
"name": "stdout",
146+
"output_type": "stream",
147+
"text": [
148+
"{'name': 'sudh', 'gender': 'male', 'salary': 10000}\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"class Human:\n",
154+
" def __init__(self,name,gender):\n",
155+
" self.name = name\n",
156+
" self.gender = gender\n",
157+
"class Employee(Human):\n",
158+
" def __init__(self,name,gender,salary):\n",
159+
" super().__init__(name,gender) \n",
160+
" self.salary = salary\n",
161+
"emp_1 = Employee('sudh','male',10000)\n",
162+
"print(emp_1.__dict__) "
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"id": "70fd3210",
168+
"metadata": {},
169+
"source": [
170+
"## 5. How is the local scope of a class different from that of a function?"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"id": "0f64f8a6",
176+
"metadata": {},
177+
"source": [
178+
"A Variable which is defined inside a function is local to that function. It is accesible from the point at which it is defined until the end of the function, and exists for as long as the function is existing.\n",
179+
"\n",
180+
"Similary a variable inside of a class also has a local variable scope. Variables which are defined in the class body (but outside all methods) are called as class level variables or class attributes. They can be referenced by their bare names within the same scope, but they can also be accessed from outside this scope if we use the attribute access operator (.) on a class or an instance of the class."
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 8,
186+
"id": "959e6eb9",
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"name": "stdout",
191+
"output_type": "stream",
192+
"text": [
193+
"you're name is sudh\n",
194+
"Name varible is not available outside hello function scope\n",
195+
"HomeSapiens\n",
196+
"HomeSapiens\n"
197+
]
198+
}
199+
],
200+
"source": [
201+
"def hello(name):\n",
202+
" name = name\n",
203+
" print(f'you\\'re name is {name}')\n",
204+
"hello('sudh')\n",
205+
"try:\n",
206+
" name\n",
207+
"except NameError:\n",
208+
" print('Name varible is not available outside hello function scope')\n",
209+
"\n",
210+
"class Person:\n",
211+
" species = \"HomeSapiens\"\n",
212+
" def __init__(self):\n",
213+
" pass\n",
214+
"print(Person.species) # Accessing species using class name\n",
215+
"Male = Person()\n",
216+
"print(Male.species) # Accessing species using instance of class"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"id": "1cebfb05",
223+
"metadata": {},
224+
"outputs": [],
225+
"source": []
226+
}
227+
],
228+
"metadata": {
229+
"kernelspec": {
230+
"display_name": "Python 3",
231+
"language": "python",
232+
"name": "python3"
233+
},
234+
"language_info": {
235+
"codemirror_mode": {
236+
"name": "ipython",
237+
"version": 3
238+
},
239+
"file_extension": ".py",
240+
"mimetype": "text/x-python",
241+
"name": "python",
242+
"nbconvert_exporter": "python",
243+
"pygments_lexer": "ipython3",
244+
"version": "3.8.8"
245+
}
246+
},
247+
"nbformat": 4,
248+
"nbformat_minor": 5
249+
}

0 commit comments

Comments
 (0)