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-wo…


This content originally appeared on DEV Community and was authored by E.Michael

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.


This content originally appeared on DEV Community and was authored by E.Michael


Print Share Comment Cite Upload Translate Updates
APA

E.Michael | Sciencx (2022-07-09T07:01:00+00:00) Object Oriented Programming with Python. Retrieved from https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/

MLA
" » Object Oriented Programming with Python." E.Michael | Sciencx - Saturday July 9, 2022, https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/
HARVARD
E.Michael | Sciencx Saturday July 9, 2022 » Object Oriented Programming with Python., viewed ,<https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/>
VANCOUVER
E.Michael | Sciencx - » Object Oriented Programming with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/
CHICAGO
" » Object Oriented Programming with Python." E.Michael | Sciencx - Accessed . https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/
IEEE
" » Object Oriented Programming with Python." E.Michael | Sciencx [Online]. Available: https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/. [Accessed: ]
rf:citation
» Object Oriented Programming with Python | E.Michael | Sciencx | https://www.scien.cx/2022/07/09/object-oriented-programming-with-python-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.