Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Reduce Bounces Up To
98% Accuracy 99%, Real-Time API, 24/7
Support, Bulk Email
Guaranteed Veri cation
≡ Menu
Home
Free eBook
Start Here
Contact
About
Like 3 Tw eet
OOP stands for Object Oriented Programming. This concept is a style of solving programming problems where
properties and behavior of a real-life object is packaged as a single entity in the code.
This style of coding enables modularizing and scaling with least amount of issues.
Python is a dynamically typed, high level interpreted programming language. Python supports several OOP features
including the following:
1. Classes in Python
Class is a blueprint of the real-life entity. In Python, it is created using the class keyword as shown in the following code
snippet.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
https://www.thegeekstuff.com/2019/03/python-oop-examples/ 1/8
6/6/2019 5 Examples to Jumpstart Object Oriented Programming in Python
In the above:
If you are new to Python, refer to this: Python Introduction Tutorial – Variable, String, Function Examples
2. Objects in Python
Once a Person class is defined you can use it to create an instance by passing values as shown below.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
if __name__ == "__main__":
p = Person("ranjeeta", 23)
print(p.name)
In the above:
p – is the name of the object that we are creating based on Person class
Even though the class has three parameters (self, name, age), we’ll still pass only name and age while creating an
object, as we don’t need to refer self here. It’s implicit.
Once an object is created, you can refer to the attributes of the object using a dot. For example, p.name refers to
the name attribute of that particular object
3. Inheritance in Python
As the name suggest, this concept is about inheriting properties from an existing entity. This increases reusability of code.
Single, Multiple and Multi-level inheritances are few of the many types supported by Python.
# Multi-level inheritance
class Manager(Employee):
def __init__(self):
pass
# Multiple Inheritance
class Enterprenaur(Person, Employee):
def __init__(self):
pass
In multiple inheritance, classes are inherited from left to right inside parenthesis, depending on Method Resolution Order
(MRO) algorithm of Python.
4. Encapsulation in Python
It is the concept of wrapping data such that the outer world has access only to exposed properties. Some properties can
be hidden to reduce vulnerability. This is an implementation of data hiding. For example, you want buy a pair of trousers
from an online site. The data that you want is its cost and availability. The number of items present and their location is
information that you are not bothered about. Hence it is hidden.
In Python this is implemented by creating private, protected and public instance variables and methods.
Private properties have double underscore (__) in the start, while protected properties have single underscore (_). By
default, all other variable and methods are public.
Private properties are accessible from within the class only and are not available for child class(if inherited). Protected
properties are accessible from within the class but are available to child class as well. All these restrictions are removed
for public properties.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def _protected_method(self):
print("protected method")
def __private_method(self):
print("privated method")
if __name__ == "__main__":
p = Person("mohan", 23)
p._protected_method() # shows a warning
p.__private_method() # throws Attribute error saying no such method exists
5. Polymorphism in Python
https://www.thegeekstuff.com/2019/03/python-oop-examples/ 3/8
6/6/2019 5 Examples to Jumpstart Object Oriented Programming in Python
This is a concept where a function can take multiple forms depending on the number of arguments or type of arguments
given to the function.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def show_salary(self):
print("Salary is unknown")
class Employee(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.salary = salary
def show_salary(self):
print("Salary is", self.salary)
if __name__ == "__main__":
p = Person("y", 23)
x = Employee("x", 20, 100000)
p.show_salary() # Salary is unknown
x.show_salary() # Salary is 100000
In the above example, super keyword is used to call a method of parent class. Both classes have the method
show_salary. Depending on the object type that makes a call to this function, the output varies.
Python has inbuilt-polymorphism functions as well. One of the simplest examples is the print function in python.
The end keyword parameter changed the functionality of print function. Hence it did not end ‘Hello There’ with an
end-line.
len([1, 2, 3, 4, 5]) in third line return an int. The print recognizes the data type and implicitly converts it to string
and prints it to the console.
Great article.. I hope you will write more articles on Python as this is most popular languages not for Dev only but
also for tester, sysadmin, data scientists and others..
Link
Leave a Comment
Name
Website
Comment
Submit
Next post: 12 AWS opsworks-cm server CLI Examples to Manage Chef or Puppet OpsWorks Server
Previous post: 5 Steps for Code Changes Only on Git Branch and Merge to Master Once Done
https://www.thegeekstuff.com/2019/03/python-oop-examples/ 5/8
6/6/2019 5 Examples to Jumpstart Object Oriented Programming in Python
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
POPULAR POSTS
CATEGORIES
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
https://www.thegeekstuff.com/2019/03/python-oop-examples/ 7/8
6/6/2019 5 Examples to Jumpstart Object Oriented Programming in Python
My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and
tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you
resolve a problem. Read more about Ramesh Natarajan and the blog.
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You
can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Support Us
https://www.thegeekstuff.com/2019/03/python-oop-examples/ 8/8