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: oop_python.md
+19-19
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3

4
4
5
-
Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.
5
+
Object Oriented Programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.
6
6
7
7
#### Table of Contents
8
8
@@ -19,9 +19,9 @@ Classes can also contain functions, called methods, available only to objects of
19
19
20
20
Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like myCar or goldenRetriever. Each object can have unique values to the properties defined in the class.
21
21
22
-
> If we have a sample class called "Car" that contains all the properties a car must have such as color, brand and model, we can create an instance of a type of car to represent a specific car.
22
+
> If we have a sample class called "Car" that contains all the properties a car must have such as color, brand, and model, we can create an instance of a type of car to represent a specific car.
23
23
>
24
-
> We can then set the value of the properties defined in the class to describe our car, whithout affecting other objects or the class template.
24
+
> We can then set the value of the properties defined in the class to describe our car, without affecting other objects or the class template.
25
25
>
26
26
> We can reuse this class to represent any number of cars.
27
27
@@ -111,7 +111,7 @@ print(kiki.career('Teacher'))
111
111
112
112
| Classes | Objects | Attributes | Methods |
113
113
| ------- | ------- | ---------- | ------- |
114
-
| As we have seen above, classes are essentially blueprints that define abstract ideas of an object. Individual objects are instantiated or created from this blueprint. | These are instances of a class, created with specific data. You can have multiple objects that use the same class. | These are the features of a class. They define the data that we would want an object to have. The state of an object is defined by the data in the object’s attributes fields. | Methods are used to represent behaviours. They perform actions that might return information about an object. When individual objects are instantiated, these objects can call the methods defined in the class. |
114
+
| As we have seen above, classes are essentially blueprints that define abstract ideas of an object. Individual objects are instantiated or created from this blueprint. | These are instances of a class, created with specific data. You can have multiple objects that use the same class. | These are the features of a class. They define the data that we would want an object to have. The state of an object is defined by the data in the object’s attributes fields. | Methods are used to represent behaviors. They perform actions that might return information about an object. When individual objects are instantiated, these objects can call the methods defined in the class. |
115
115
116
116
117
117
## The Four Principles of Object-oriented Programming
@@ -125,7 +125,7 @@ The four pillars of OOP in python are:
125
125
126
126
## Inheritance
127
127
128
-
This principle allows other classes to acquire the features of other classes. In other words, one class extends its attributes and behaviours to another class. The class in which the basic attributes and behaviours are defined is called the 'parent' class or the 'base' class. The class that inherits (or acquires) the attributes and behaviours of the parent class is called the 'child' class or the 'subclass'. The essence of inheritance is to promote code reusability.
128
+
This principle allows other classes to acquire the features of other classes. In other words, one class extends its attributes and behaviors to another class. The class in which the basic attributes and behaviors are defined is called the 'parent' class or the 'base' class. The class that inherits (or acquires) the attributes and behaviors of the parent class is called the 'child' class or the 'subclass'. The essence of inheritance is to promote code reusability.
129
129
130
130
131
131
```python
@@ -193,7 +193,7 @@ class Child(Parent):
193
193
returnf'Child: {self.username}, {self.age}''
194
194
```
195
195
196
-
We have modified the `__repr__()` function for the `Child` class to have its own string besides the dynamic `username` and `email` values. The `Child` class has an additional `age` attribute that does not exist in the parent. Let us see how polymorphism works.
196
+
We have modified the `__repr__()` function for the `Child` class to have its string besides the dynamic `username` and `email` values. The `Child` class has an additional `age` attribute that does not exist in the parent. Let us see how polymorphism works.
197
197
198
198
```python
199
199
$ python
@@ -207,14 +207,14 @@ Parent: harry
207
207
Child: muthoni, 3
208
208
```
209
209
210
-
Notice that the parent's `__repr__()` function has been overriden by the child's. This is because the child defined its own `__repr__()` function. Additionally, the `age` attribute is only present in the child class.
210
+
Notice that the parent's `__repr__()` function has been overridden by the child's. This is because the child defined its own `__repr__()` function. Additionally, the `age` attribute is only present in the child class.
211
211
212
212
213
213
## Encapsulation
214
214
215
-
This principle promotes the need to hide data. From the word 'encapsulate', we learn that it means to 'enclose something in or as if in a capsule'. Synonyms associated with encapsulate are enclose, encase, confine, envelop etc. I hope you get the idea.
215
+
This principle promotes the need to hide data. From the word 'encapsulate', we learn that it means to 'enclose something in or as if in a capsule. Synonyms associated with encapsulate are enclosed, encase, confine, envelop, etc. I hope you get the idea.
216
216
217
-
The creation of classes is by default encapsulation. This is because we are limiting access to the data from the outside world. This data can only be accessed using the said class. The entire process of encapsulation is also called 'information hiding'.
217
+
The creation of classes is by default encapsulation. This is because we are limiting access to data from the outside world. This data can only be accessed using the said class. The entire process of encapsulation is also called 'information hiding.
218
218
219
219
```python
220
220
classParent():
@@ -226,7 +226,7 @@ class Parent():
226
226
returnf'Parent: {self.username}'
227
227
```
228
228
229
-
In order for us to access any information about a parent, we will have to use the `Parent` class. We will instantiate an object, then use it to access whatever data we want.
229
+
For us to access any information about a parent, we will have to use the `Parent` class. We will instantiate an object, then use it to access whatever data we want.
230
230
231
231
Besides restricting access to the class data, we can more specifically limit access to individual variables and methods to prevent accidental data modification. Whenever we are working with the class and dealing with sensitive data, providing access to all variables used within the class is not a good choice.
232
232
@@ -334,7 +334,7 @@ How can we access a private variable? We will look at three ways:
334
334
335
335
### Using public methods
336
336
337
-
Here, we can define an instance method which will have access to the private variable. Remember, we said that a private variable can only be accessed within its own class.
337
+
Here, we can define an instance method that will have access to the private variable. Remember, we said that a private variable can only be accessed within its class.
338
338
339
339
```python
340
340
classParent():
@@ -363,7 +363,7 @@ The method `show_phone()` in an instance method of the `Parent` class. In it, we
363
363
### Name mangling
364
364
365
365
366
-
To 'mangle' means to destroy or to severely damage by tearing or crushing. Other words used to mean mangle include mutilate, crush, disfigure etc. In OOP, name mangling is used to cleverly overcome the restriction of ecapsulation.
366
+
To 'mangle' means to destroy or to severely damage by tearing or crushing. Other words used to mean mangle include mutilate, crush, disfigure, etc. In OOP, name mangling is used to cleverly overcome the restriction of encapsulation.
Typically, name mangling involves adding a preceding underscore to a class name then appending the double underscore private variable to it in order to gain access the variable's data. The format used is:
392
+
Typically, name mangling involves adding a preceding underscore to a class name and then appending the double underscore private variable to it to gain access to the variable's data. The format used is:
393
393
394
394
```python
395
395
object._classname__privateVariable
@@ -449,15 +449,15 @@ You might be wondering how `phone` has become private yet when defining the clas
449
449
450
450
### Setter Method
451
451
452
-
Now that `phone` is private by the help of our getter method, we can focus on setting its value. A setter method uses a decorator whose name is similar to the getter method, then append the word `setter`. The method name is also similar to the getter method.
452
+
Now that `phone` is private with the help of our getter method, we can focus on setting its value. A setter method uses a decorator whose name is similar to the getter method, then append the word `setter`. The method name is also similar to the getter method.
453
453
454
454
```python
455
455
@get_phone.setter
456
456
defget_phone(self, phone):
457
457
self.__get_phone = phone
458
458
```
459
459
460
-
Again, the naming of the setter method is similar to that of the getter method. We know that it is a setter method because (1) it has the `setter` decorator, (2) we are assigning `phone` to `__get_phone`, our getter method. This method takes additional argurments compared to that of the getter method.
460
+
Again, the naming of the setter method is similar to that of the getter method. We know that it is a setter method because (1) it has the `setter` decorator, (2) we are assigning `phone` to `__get_phone`, our getter method. This method takes additional arguments compared to that of the getter method.
461
461
462
462
In an active Python interpreter, you will notice that the value of `phone` can only be accessed using the getter method which has been set using the setter method. It is only possible to alter the value of `phone` using the `get_phone` setter method.
463
463
@@ -484,9 +484,9 @@ The deletion format is `del object.deleterMethod`.
484
484
485
485
## Abstraction
486
486
487
-
Abstraction is used to hide the internal functionality of a process from the users. The users only interact with the basic implementation of the function, but inner working is hidden. For example, we mostly know that to increase the volume of a TV using a remote control, all we need to do is press the "+" button and the volume will go up. We do not know HOW the volume goes up, because that process has been hidden from us.
487
+
Abstraction is used to hide the internal functionality of a process from the users. The users only interact with the basic implementation of the function, but the inner working is hidden. For example, we mostly know that to increase the volume of a TV using a remote control, all we need to do is press the "+" button and the volume will go up. We do not know HOW the volume goes up, because that process has been hidden from us.
488
488
489
-
In Python, an abstract class can be considered as a blueprint for other classes. It allows one to create a set of methods that MUST be created within any child classes built from the abstract class.
489
+
In Python, an abstract class can be considered a blueprint for other classes. It allows one to create a set of methods that MUST be created within any child classes built from the abstract class.
490
490
491
491
```python
492
492
from abc importABC, abstractmethod
@@ -511,7 +511,7 @@ $ python3
511
511
I am Muthoni
512
512
```
513
513
514
-
We begin by first importing the ABC from the Abstract Base Class (ABC) because Python does not provide abstract classes. ABC works by decorating methods of the base class as abstract. A method becomes abstract when decorated with the keyword `@abstractmethod`. This method MUST then be used by all child classes.
514
+
We begin by first importing the ABC from the Abstract Base Class (ABC) because Python does not provide abstract classes. ABC works by decorating methods of the base class as abstract. A method becomes abstract when decorated with the keyword `@abstractmethod`. This method MUST then be used by all child classes.
515
515
516
516
In our example above, the `Parent` class defines an abstract method called `about_me` with its own custom `print()` statement. The `Child` class inherits the `Parent` class and overrides the `print()` statement with its own. If the child class tries not to use the defined abstract method, then an error is raised.
517
517
@@ -538,4 +538,4 @@ $ python3
538
538
>>> child.something_else()
539
539
TypeError: Can't instantiate abstract class Child with abstract methods about_me
540
540
```
541
-
The `Child` class is required to have its own implememtation of the abstract method.
541
+
The `Child` class is required to have its implementation of the abstract method.
0 commit comments