DEV Community 👩‍💻👨‍💻

Cover image for Object Oriented Programming with Python
EzeanaMichael
EzeanaMichael

Posted on

Object Oriented Programming with Python

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

Output:
Image description

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

Output:
Image description

Classes can also contain specific attributes within them which can be accessed by the objects and class itself.
Code:
Image description

Output:
Image description

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

Output:
Image description

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

Output:
Image description

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.

Top comments (0)

🌚 Life is too short to browse without dark mode