You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-25
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@ If we look at the features of the Python Programming Language. We find that, Pyt
18
18
19
19
One of the main features of Python is; it has **Effective approach to object-oriented programming**. So, let's first find out what does `Object-Oriented-Programming` means?
20
20
21
-
OOP is the programming paradigm where the real-life object is at the center of all the concepts and methodology used to perform a task or a process. Instead of calling the procedures and functions to calculate the data as and when required, OOP uses a blueprint to create an object of a class and bundle data and functionality to it.
21
+
OOP is the programming paradigm where the real-life object is at the center of all the concepts and methodology used to perform a task or a process. Instead of calling the procedures and functions to calculate the data as and when required, OOP uses a blueprint to create an object of a class and bundle data and functionality to it.
22
+
23
+
Everything in Python is an object. They have some data and functionality associated with them. So all the Python objects like **list**, **int**, **str**, **tuple**, **functions**, **sys** etc are objects.
22
24
23
25
---
24
26
@@ -62,36 +64,40 @@ OOP is the programming paradigm where the real-life object is at the center of a
62
64
63
65
## Definition of class:
64
66
65
-
A python class is like a **blueprint to create a new object**. The objects having similar set of attributes and behaviors goes to the same class. For example: Cars, TV, Employees, Students, Smart phones. All the new object of a car class shares same similar attributes and methods.
67
+
A python class is like a **blueprint to create a new object**. The objects having similar set of attributes and behaviors goes to the same class. For example: Cars, TV, Employees, Students, Smart phones. All the new objects of a class shares similar attributes and methods.
66
68
67
69
---
68
70
69
71
## Reason for creating a class:
70
-
> To accumulate the data of similar properties and methods together. By creating a new object we add this new instance, or any future instance, to a particular bundle. That bundle is called a `class`. If we have the same set of objects with similar attributes and functionalities we should create a class. Also, class supports inheritance. With classes we can reuse our codes or the codes written by other programmers. (Packages, modules and frameworks)
72
+
To accumulate the data of similar properties and methods together.
73
+
74
+
By creating a new object we add this new instance, or any future instance, to a particular bundle. That bundle is called a `class`. If we have the same set of objects with similar attributes and functionalities we should create a class. Also, class supports inheritance. With classes we can reuse our codes or the codes written by other programmers. (Packages, modules and frameworks)
71
75
72
76
---
73
77
74
78
## Naming a class
75
79
76
-
To name a `class` in Python the convention is to start with capital letter like: Animal, Employee, ImageDetector.
80
+
To name a `class` in Python the convention is to start with capital letter like: Animal, Employee. Similarly if the class is made up with two or more words, each word has the first letter capitalize like: ImageDetector, PointRotationCalculator etc.
77
81
78
82
---
79
83
80
84
## class keyword and __init__ method
81
-
Everything in Python is a class. like **list**, **int**, **str**, **tuple**, **functions**, **sys** etc. To create a user defined object we use **class** keyword in Python. The special method **__init__** is used to initialize an object of that class.
85
+
To create a user defined object we use **class** keyword in Python. The special method **__init__** is used to initialize an object of that class.
86
+
87
+
Let's look at an example of creating a user defined class in Python.
82
88
83
89
### An example of a `Car class`:
84
90
Let's say, we need to create a record of cars manufactured by the Tata Motors. The data and data related to car are as mentioned below.
85
91
86
92
MAKE = "TATA Motors"
87
93
Year = 2022
88
94
89
-
|Model|Color|Speed|Month|
90
-
|-----|---|---|---|
91
-
|Nexon|Grey|160|November|
92
-
|Nexon|Black|160| November|
93
-
|Harrier|Black|180| December|
94
-
|Harrier|Dark Grey|180| December|
95
+
|Model|Color|Speed|
96
+
|-----|---|---|
97
+
|Altroz|Blue|160|
98
+
|Nexon|Grey|160|
99
+
|Harrier|Dark Grey|180|
100
+
|Safari|Black|170|
95
101
96
102
**A table containing details of each Car. The row of this table represents the each instance of a class and columns represents attributes associated with an instance of the Car class**.
97
103
@@ -102,11 +108,10 @@ class Car:
102
108
MAKE="TATA Motors"
103
109
year =2022
104
110
105
-
def__init__(self, model, color, speed, month):
111
+
def__init__(self, model, color, speed):
106
112
self.model = model
107
113
self.color = color
108
114
self.speed = speed
109
-
self.month = month
110
115
111
116
defget_speed(self):
112
117
returnf"The max-speed of {Car.MAKE} car '{self.model}' is {self.speed} km/h"
@@ -123,18 +128,23 @@ class Car:
123
128
print(f"Color: {self.color}")
124
129
print(f"Max Speed: {self.speed}")
125
130
print(f"Manufacturing Year: {Car.year}")
126
-
print(f"Manufacturing Month: {self.month}")
131
+
132
+
@classmethod
133
+
defchange_year(cls, new_year):
134
+
cls.year = new_year
135
+
returncls.year
127
136
128
137
129
-
c1 = Car("Harrier", "grey", 180, 'December')
130
-
c1.description()
131
-
print(c1.get_speed())
138
+
car1 = Car("Harrier", "grey", 180)
139
+
car2 = Car("Nexon", "Dark Grey", 160)
140
+
car1.description()
141
+
print(car1.get_speed())
132
142
```
133
143
134
144
---
135
145
136
146
## Instance
137
-
> An object of a class (constructed or instantiated from the class) is called the instance of that class. From our car class example (above):
147
+
An object of a class (constructed or instantiated from the class) is called the instance of that class. From our car class example (above):
138
148
139
149
If we want to add another record into our Car table above, so we will **create new instance of the car class**, it means we already have 4 instances(rows) in our table, and now let's create the fifth one.
140
150
@@ -143,7 +153,7 @@ If we want to add another record into our Car table above, so we will **create n
143
153
## Attributes and methods:
144
154
From the above example of a Car class we can deduce:
@@ -155,12 +165,12 @@ The variables associated to a particular instance of a class.Also we can say, th
155
165
156
166
We can declared them in the special method of a class named **__init__**.
157
167
158
-
In our example of Car class, every instance of the Car class have their own set of model, color, speed, month associated with it. So, these are the instance variables(attributes)
168
+
In our example of Car class, every instance of the Car class have their own set of model, color, speed associated with it. So, these are the instance variables(attributes)
159
169
160
170
We use dot notation with class instance to access them. e.g.
161
171
```python
162
172
# create a new instance
163
-
car1 = Car("Harrier", "grey", 180, 'December')
173
+
car1 = Car("Harrier", "grey", 180)
164
174
165
175
# access the name and speed of this instance
166
176
print(car.name)
@@ -198,7 +208,7 @@ In our example of Car class, the get_speed(), apply_brakes() are the methods app
198
208
199
209
To access the instance methods:
200
210
```python
201
-
car = Car("Harrier", "grey", 180, 'December')
211
+
car = Car("Harrier", "grey", 180)
202
212
car.description()
203
213
print(car.get_speed())
204
214
```
@@ -214,8 +224,8 @@ In our example of Car class, if we need to work upon the class variables like ye
0 commit comments