Skip to content

Commit 4f07743

Browse files
committed
Update read me
1 parent bccae37 commit 4f07743

File tree

4 files changed

+175
-121
lines changed

4 files changed

+175
-121
lines changed

README.md

+36-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ If we look at the features of the Python Programming Language. We find that, Pyt
1818

1919
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?
2020

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.
2224

2325
---
2426

@@ -62,36 +64,40 @@ OOP is the programming paradigm where the real-life object is at the center of a
6264

6365
## Definition of class:
6466

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.
6668

6769
---
6870

6971
## 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)
7175

7276
---
7377

7478
## Naming a class
7579

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.
7781

7882
---
7983

8084
## 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.
8288

8389
### An example of a `Car class`:
8490
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.
8591

8692
MAKE = "TATA Motors"
8793
Year = 2022
8894

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|
95101

96102
**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**.
97103

@@ -102,11 +108,10 @@ class Car:
102108
MAKE = "TATA Motors"
103109
year = 2022
104110

105-
def __init__(self, model, color, speed, month):
111+
def __init__(self, model, color, speed):
106112
self.model = model
107113
self.color = color
108114
self.speed = speed
109-
self.month = month
110115

111116
def get_speed(self):
112117
return f"The max-speed of {Car.MAKE} car '{self.model}' is {self.speed} km/h"
@@ -123,18 +128,23 @@ class Car:
123128
print(f"Color: {self.color}")
124129
print(f"Max Speed: {self.speed}")
125130
print(f"Manufacturing Year: {Car.year}")
126-
print(f"Manufacturing Month: {self.month}")
131+
132+
@classmethod
133+
def change_year(cls, new_year):
134+
cls.year = new_year
135+
return cls.year
127136

128137

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())
132142
```
133143

134144
---
135145

136146
## 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):
138148

139149
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.
140150

@@ -143,7 +153,7 @@ If we want to add another record into our Car table above, so we will **create n
143153
## Attributes and methods:
144154
From the above example of a Car class we can deduce:
145155

146-
- Attributes: model, color, speed, month
156+
- Attributes: model, color, speed
147157
- Methods: get_speed(), accelerate(), apply_break(), description().
148158

149159
---
@@ -155,12 +165,12 @@ The variables associated to a particular instance of a class.Also we can say, th
155165

156166
We can declared them in the special method of a class named **__init__**.
157167

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)
159169

160170
We use dot notation with class instance to access them. e.g.
161171
```python
162172
# create a new instance
163-
car1 = Car("Harrier", "grey", 180, 'December')
173+
car1 = Car("Harrier", "grey", 180)
164174

165175
# access the name and speed of this instance
166176
print(car.name)
@@ -198,7 +208,7 @@ In our example of Car class, the get_speed(), apply_brakes() are the methods app
198208

199209
To access the instance methods:
200210
```python
201-
car = Car("Harrier", "grey", 180, 'December')
211+
car = Car("Harrier", "grey", 180)
202212
car.description()
203213
print(car.get_speed())
204214
```
@@ -214,8 +224,8 @@ In our example of Car class, if we need to work upon the class variables like ye
214224
cls.year = new_year
215225
return cls.year
216226

217-
car1 = Car("Harrier", "grey", 180, 'December')
218-
car2 = Car("Nexon", "Dark Grey", 160, 'January')
227+
car1 = Car("Harrier", "grey", 180)
228+
car2 = Car("Nexon", "Dark Grey", 160)
219229

220230
year = Car.change_year(2023)
221231
print(year)
@@ -256,8 +266,9 @@ print(isinstance(cat1, Cat))
256266
True
257267
```
258268

259-
### Help(): Python docs
269+
### Help():
260270
```python
271+
# From the examples/2_2_animal_class.py
261272
print(help(cat1))
262273
```
263274
### Output:

examples/4_1_car_class.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ class Car:
22
MAKE = "TATA Motors"
33
year = 2022
44

5-
def __init__(self, model, color, speed, month):
5+
def __init__(self, model, color, speed):
66
self.model = model
77
self.color = color
88
self.speed = speed
9-
self.month = month
109

1110
def get_speed(self):
1211
return f"The max-speed of {Car.MAKE} car '{self.model}' is {self.speed} km/h"
@@ -23,16 +22,15 @@ def description(self):
2322
print(f"Color: {self.color}")
2423
print(f"Max Speed: {self.speed}")
2524
print(f"Manufacturing Year: {Car.year}")
26-
print(f"Manufacturing Month: {self.month}")
2725

2826
@classmethod
2927
def change_year(cls, new_year):
3028
cls.year = new_year
3129
return cls.year
3230

3331

34-
car1 = Car("Harrier", "grey", 180, 'December')
35-
car2 = Car("Nexon", "Dark Grey", 160, 'January')
32+
car1 = Car("Harrier", "grey", 180)
33+
car2 = Car("Nexon", "Dark Grey", 160)
3634
car1.description()
3735
print(car1.get_speed())
3836

examples/4_2_car_class.py

+32-91
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,45 @@
1-
from ast import literal_eval
2-
3-
41
class Car:
5-
'''A base car class.
6-
Store the values of color, year, price and dimension.
7-
Dimention attribute is not necessarily required to instentiate the car class.
8-
'''
9-
# class attribute... applied to all class instances
10-
discount_percentage = 0.1
2+
MAKE = "TATA Motors"
3+
year = 2022
114

12-
def __init__(self, color, year, price, dimension=None):
5+
def __init__(self, model, color, speed, month):
6+
self.model = model
137
self.color = color
14-
self.year = year
15-
self.price = price
16-
17-
if dimension is None:
18-
self.dimension = (None, None, None)
19-
else:
20-
self.dimension = dimension
21-
assert isinstance(self.dimension, tuple)
22-
assert len(self.dimension) == 3
23-
24-
def display_details(self):
25-
'''Display car details. If not dimention then display none.'''
26-
car = {
27-
'color': self.color,
28-
'year': self.year,
29-
'price': self.price,
30-
'dimension': self.dimension,
31-
}
32-
return car
8+
self.speed = speed
9+
self.month = month
3310

34-
@property
35-
def discount_amount(self):
36-
'''An attribute of Car class calculated by a method (using property decorator)
37-
A pythonic way of getter.
38-
'''
39-
return self.price * self.discount_percentage
11+
def get_speed(self):
12+
return f"The max-speed of {Car.MAKE} car '{self.model}' is {self.speed} km/h"
4013

41-
@property
42-
def length(self):
43-
return self.dimension[0]
14+
def accelerate(self):
15+
return "The vehicle is accelerating"
4416

45-
@property
46-
def width(self):
47-
return self.dimension[1]
17+
def apply_brake(self):
18+
return "The vehicle is de-accelerating"
4819

49-
@property
50-
def height(self):
51-
return self.dimension[2]
52-
53-
def discount_price(self):
54-
return self.price - self.discount_amount
55-
56-
def is_smallcar(self):
57-
decider_length = 3800
58-
return True if self.length < decider_length else False
59-
60-
@classmethod
61-
def set_discount_percentage(cls, discount_percentage):
62-
cls.discount_percentage = discount_percentage
20+
def description(self):
21+
print(f"Company name: {Car.MAKE}")
22+
print(f"Model: {self.model}")
23+
print(f"Color: {self.color}")
24+
print(f"Max Speed: {self.speed}")
25+
print(f"Manufacturing Year: {Car.year}")
26+
print(f"Manufacturing Month: {self.month}")
6327

6428
@classmethod
65-
def from_file(cls, file_data):
66-
created_classes = []
67-
for line in file_data:
68-
year, price, dimension, color = int(line[0]), int(
69-
line[1]), tuple(literal_eval(line[2])), line[3]
70-
created_classes.append(cls(color, year, price, dimension))
71-
72-
return created_classes
73-
74-
75-
class DieselCar(Car):
76-
fuel_type = 'Diesel'
77-
78-
def __init__(self, color, year, price, dimension=None, injector=4):
79-
super().__init__(color, year, price, dimension)
80-
self.injector = injector
81-
82-
83-
class ElectricCar(Car):
84-
pass
85-
29+
def change_year(cls, new_year):
30+
cls.year = new_year
31+
return cls.year
8632

87-
if __name__ == '__main__':
88-
dcar_1 = DieselCar('white', 2023, 1500000, (4500, 1200, 1300), 4)
89-
# car1 = Car('black', 2022, 1000000, (4500, 1100, 1200))
90-
# car2 = Car('grey', 2022, 1100000, (3700, 1000, 1100))
91-
# print(car1.discount_percentage)
92-
# print(car2.discount_percentage)
9333

94-
# Car.set_discount_percentage(0.15)
95-
# print(car1.discount_percentage)
34+
car1 = Car("Harrier", "grey", 180, 'December')
35+
car2 = Car("Nexon", "Dark Grey", 160, 'January')
36+
car1.description()
37+
print(car1.get_speed())
9638

97-
# with open('file_data.txt', 'r') as fl:
98-
# lines = fl.readlines()
99-
# lines = [line.rstrip('\n').split("-") for line in lines]
100-
# print(lines)
101-
# created_objects = Car.from_file(lines)
39+
year = Car.change_year(2023)
40+
print(year)
41+
print(car1.year)
42+
print(car2.year)
10243

103-
# for obj in created_objects:
104-
# print(obj.display_details())
44+
print(dir(car1))
45+
print(dir(Car))

0 commit comments

Comments
 (0)