Skip to content

Commit e09b2b4

Browse files
committed
Repository Updated.
1 parent fb1a3fd commit e09b2b4

File tree

4 files changed

+1432
-0
lines changed

4 files changed

+1432
-0
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "61955b52-57eb-495f-8bbe-85b585e862b2",
6+
"metadata": {},
7+
"source": [
8+
"## Types of Methods\n",
9+
"Inside a Python class, you can define various types of methods, each serving a specific purpose. Here are some commonly used types of methods in Python classes:"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"id": "eda30541-6bfe-4357-bbb9-60c8c7691770",
15+
"metadata": {},
16+
"source": [
17+
"### Instance Method\n",
18+
"Instance methods are the most common type of methods in Python classes. These are so-called instance methods because they can access the unique data of their instance. If you have two objects each created from a car class, then they each may have different properties. They may have different colors, engine sizes, seats, and so on.\n",
19+
"\n",
20+
"Instance methods must have self as a parameter, but you don't need to pass this in every time. Self is another Python special term. Inside any instance method, you can use self to access any data or methods that may reside in your class. You won't be able to access them without going through self.\n",
21+
"\n",
22+
"Finally, as instance methods are the most common, there's no decorator needed. Any method you create will automatically be created as an instance method unless you tell Python otherwise.\n",
23+
"\n",
24+
"**Usage:**\n",
25+
"* Performing calculations or operations on instance attributes.\n",
26+
"* Modifying the state of an individual instance.\n",
27+
"* Implementing instance-specific functionality."
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 1,
33+
"id": "a9eac279-075d-4212-a7fb-ed833997abb3",
34+
"metadata": {
35+
"tags": []
36+
},
37+
"outputs": [],
38+
"source": [
39+
"# Creating a Circle class with instance methods \n",
40+
"class Circle:\n",
41+
" _pi = 3.14159 # protected data member\n",
42+
" \n",
43+
" # constructor\n",
44+
" def __init__(self, radius):\n",
45+
" self.radius = radius\n",
46+
" \n",
47+
" # instance method \n",
48+
" def calculate_area(self):\n",
49+
" area = self._pi * (self.radius)**2\n",
50+
" return area\n",
51+
" \n",
52+
" # instance method\n",
53+
" def calculate_perimeter(self):\n",
54+
" perimeter = 2 * self._pi * self.radius\n",
55+
" return perimeter"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"id": "5577549c-40e7-4c08-b3a5-715e18953ae9",
62+
"metadata": {
63+
"tags": []
64+
},
65+
"outputs": [
66+
{
67+
"name": "stdout",
68+
"output_type": "stream",
69+
"text": [
70+
"The area of the circle: 78.53975\n",
71+
"The perimeter of the circle: 31.4159\n"
72+
]
73+
}
74+
],
75+
"source": [
76+
"# Creating an object from Circle class\n",
77+
"circle1 = Circle(5)\n",
78+
"# Calling the instance methods using the object\n",
79+
"print(\"The area of the circle:\", circle1.calculate_area())\n",
80+
"print(\"The perimeter of the circle:\", circle1.calculate_perimeter())"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"id": "988ab6b0-d96a-4cbb-ac44-e59d2782391d",
86+
"metadata": {},
87+
"source": [
88+
"### Class Methods\n",
89+
"Class methods are bound to the class itself rather than the instance. They have access to the class-level attributes and can be called using the class name or an instance. Class methods are defined using the **@classmethod** decorator, and the first parameter is conventionally named cls to represent the class.\n",
90+
"\n",
91+
"**Usage:**\n",
92+
"* Modifying or accessing class-level variables.\n",
93+
"* Performing operations that are relevant to the class as a whole.\n",
94+
"* Providing alternative ways to create instances of a class."
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 3,
100+
"id": "2226d0ca-cbcb-46b4-a57d-f95804429c68",
101+
"metadata": {
102+
"tags": []
103+
},
104+
"outputs": [],
105+
"source": [
106+
"# Creating a rectangle class\n",
107+
"class Rectangle:\n",
108+
" width = 0 # class-level variable\n",
109+
" height = 0 # class-level variable\n",
110+
" \n",
111+
" def __init__(self, width, height):\n",
112+
" self.width = width\n",
113+
" self.height = height\n",
114+
" \n",
115+
" def calculate_area(self):\n",
116+
" area = self.width * self.height\n",
117+
" return area\n",
118+
" \n",
119+
" @classmethod\n",
120+
" def change_size(cls, new_width, new_height):\n",
121+
" cls.width = new_width\n",
122+
" cls.height = new_height"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 4,
128+
"id": "3ffce423-c27c-4b6b-9594-20b613ccc9c0",
129+
"metadata": {
130+
"tags": []
131+
},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"The area of the rectangle: 50\n"
138+
]
139+
}
140+
],
141+
"source": [
142+
"# Creating an instance from the rectangle class\n",
143+
"rectangle1 = Rectangle(10, 5)\n",
144+
"# Calling the instance method\n",
145+
"print(\"The area of the rectangle:\", rectangle1.calculate_area())"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 5,
151+
"id": "505260cb-fcdb-49e7-a073-c57b14b85381",
152+
"metadata": {
153+
"tags": []
154+
},
155+
"outputs": [
156+
{
157+
"name": "stdout",
158+
"output_type": "stream",
159+
"text": [
160+
"0\n",
161+
"0\n"
162+
]
163+
}
164+
],
165+
"source": [
166+
"# Checking the class-level variable\n",
167+
"print(Rectangle.width)\n",
168+
"print(Rectangle.height)"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 6,
174+
"id": "ad63638f-2482-46a8-a5c0-527ffea7d476",
175+
"metadata": {
176+
"tags": []
177+
},
178+
"outputs": [
179+
{
180+
"name": "stdout",
181+
"output_type": "stream",
182+
"text": [
183+
"6\n",
184+
"4\n"
185+
]
186+
}
187+
],
188+
"source": [
189+
"# Modifying the class-level variable using class method\n",
190+
"Rectangle.change_size(6, 4)\n",
191+
"# Checking the new class-level variable\n",
192+
"print(Rectangle.width)\n",
193+
"print(Rectangle.height)"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"id": "33d0a487-5ee0-4ae1-985b-2b9aabe7b102",
199+
"metadata": {},
200+
"source": [
201+
"### Static Methods\n",
202+
"Static methods, much like class methods, are methods that are bound to a class rather than its object.\n",
203+
"They do not require a class instance creation. So, they are not dependent on the state of the object.\n",
204+
"\n",
205+
"The difference between a static method and a class method is:\n",
206+
"* The static method knows nothing about the class and just deals with the parameters.\n",
207+
"* The class method works with the class since its parameter is always the class itself.\n",
208+
"\n",
209+
"**Usage:**\n",
210+
"* Implementing utility functions that are logically related to the class.\n",
211+
"* Performing calculations or operations that don't require access to instance or class attributes.\n",
212+
"* Grouping related functions together within a class for organizational purposes."
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 7,
218+
"id": "4615b0a5-608c-4feb-b64a-003f49e8dd91",
219+
"metadata": {
220+
"tags": []
221+
},
222+
"outputs": [],
223+
"source": [
224+
"# Creating a MathUtils class\n",
225+
"class MathUtils:\n",
226+
" \n",
227+
" @staticmethod\n",
228+
" def add(a, b, *args):\n",
229+
" sum_of_numbers = a + b\n",
230+
" for i in args:\n",
231+
" sum_of_numbers += i\n",
232+
" return sum_of_numbers\n",
233+
" \n",
234+
" @staticmethod\n",
235+
" def multiply(a, b, *args):\n",
236+
" product_of_numbers = a * b\n",
237+
" for i in args:\n",
238+
" product_of_numbers *= i\n",
239+
" return product_of_numbers"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 8,
245+
"id": "269fd842-637c-422f-8603-8b810e8ecd6b",
246+
"metadata": {
247+
"tags": []
248+
},
249+
"outputs": [
250+
{
251+
"data": {
252+
"text/plain": [
253+
"5"
254+
]
255+
},
256+
"execution_count": 8,
257+
"metadata": {},
258+
"output_type": "execute_result"
259+
}
260+
],
261+
"source": [
262+
"# Using the static methods\n",
263+
"MathUtils.add(2, 3)"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 9,
269+
"id": "b8cae66a-81d0-45cf-bbfe-c1231a637341",
270+
"metadata": {
271+
"tags": []
272+
},
273+
"outputs": [
274+
{
275+
"data": {
276+
"text/plain": [
277+
"14"
278+
]
279+
},
280+
"execution_count": 9,
281+
"metadata": {},
282+
"output_type": "execute_result"
283+
}
284+
],
285+
"source": [
286+
"MathUtils.add(2, 3, 4, 5)"
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": 10,
292+
"id": "042ac416-9312-405a-b5d5-eacbad43e797",
293+
"metadata": {
294+
"tags": []
295+
},
296+
"outputs": [
297+
{
298+
"data": {
299+
"text/plain": [
300+
"6"
301+
]
302+
},
303+
"execution_count": 10,
304+
"metadata": {},
305+
"output_type": "execute_result"
306+
}
307+
],
308+
"source": [
309+
"MathUtils.multiply(2, 3)"
310+
]
311+
},
312+
{
313+
"cell_type": "code",
314+
"execution_count": 11,
315+
"id": "ef73de77-8603-465d-b173-ca4c8c54f3f9",
316+
"metadata": {
317+
"tags": []
318+
},
319+
"outputs": [
320+
{
321+
"data": {
322+
"text/plain": [
323+
"120"
324+
]
325+
},
326+
"execution_count": 11,
327+
"metadata": {},
328+
"output_type": "execute_result"
329+
}
330+
],
331+
"source": [
332+
"MathUtils.multiply(2, 3, 4, 5)"
333+
]
334+
}
335+
],
336+
"metadata": {
337+
"kernelspec": {
338+
"display_name": "Python 3 (ipykernel)",
339+
"language": "python",
340+
"name": "python3"
341+
},
342+
"language_info": {
343+
"codemirror_mode": {
344+
"name": "ipython",
345+
"version": 3
346+
},
347+
"file_extension": ".py",
348+
"mimetype": "text/x-python",
349+
"name": "python",
350+
"nbconvert_exporter": "python",
351+
"pygments_lexer": "ipython3",
352+
"version": "3.10.9"
353+
}
354+
},
355+
"nbformat": 4,
356+
"nbformat_minor": 5
357+
}

0 commit comments

Comments
 (0)