Object Oriented Programming with Python
Tags: beginners, tutorial, programming, python
Published: 2022-07-09 07:01:00

Python programming language supports procedural and functional programming as well as Object-oriented programming, In this article, I'll be giving the basics of object-oriented programming which makes the use of classes and objects to represent real-world entities in programming.
A class can be written in form:
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lglb3fx1d700ywu7pr2e.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b43hl6n3x233u0g17g6u.png)

From the above code, you can create a class by typing "class class-name" and then defining the first function as written "__init__", it is used to allow the class to initialize the attributes of the class using the class name as a function.

The "self" argument is written to link the class to the respective other arguments unlike functions are normally done, it is written in "self.arg=arg" to specify where the argument is pointing to (the class) but the self is not a specified argument by the user as seen in the code only the occupation and age are written, furthermore, the data added can be stored in a variable and the needed attribute of the class can be printed as shown above.

The objects are John and Adam whose attributes are in order of the defined class and can be assessed by using the dot syntax.

Classes also have methods that can be created by using the "def" keyword like creating another function of the class and the first parameter must also be the self parameter, which therefore can also be called using a function-like syntax
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bljf5068xneq3u7h4f23.png)


Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/am7sp4ydjns6cjg7q512.png)

Classes can also contain specific attributes within them which can be accessed by the objects and class itself.
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2ujvsr3dq8pz0p8h7bc.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgn1h5jhdjn7gyc6d2lp.png)

**Inheritance**
Classes can access or derive attributes from another class this is known as Inheritance. The classes that are inherited from can be called the superclass and the class that is inherited from the superclass is called the subclass. A subclass is created by typing in the format"class classname(superclass):" as shown below.
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzur0kufbfvtiweyhwng.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f01nepdxnd7chnhen1vl.png)

The class department is a subclass to the class job which has the arguments occupation and age, the arguments of the parent class can be used in the subclass in a function-like manner and the function inside the subclass can be called and shown above.

The "__init__" parameter can be written in the subclass as it is in the parent class as well. which creates the possibility to write a code like this:

Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b9rn2fmc2u7hohmsmgj.png)


Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eltznfqey0u2mm61xrx.png)

The code above shows the class snake and subclass python the second__init__ includes the function parameters in the superclass and subclass, the snake.__init__ helps to link the parameters of the superclass to the parameters of the subclass which are in the superclass as the same with the addition of the parameters in the subclass.

There are more uses of object-oriented programming to represent real-world entities which include encapsulation, polymorphism, etc.
Read, like, share, and comment have a wonderful day.
Here I come to the end of the beginners series programming with python, I'll make a few edits here and there, and when I've gotten more knowledge and inspiration continue this to a more advanced one.