
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialObject oriented programming is one of the various programming paradigms in which programming languages are classified. Early programming languages developed in 50s and 60s are recognized as procedural (or procedure oriented) languages. Object oriented approach was proposed to overcome limitations of procedure oriented programming language. So, we first try to understand these limitations.
Every function is written in such a way that it can interface with other functions in the program. Data belonging to a function can be easily shared with other in the form of arguments, and called function can return its result back to calling function.
This approach seems to be sound one, but as the scope of program becomes larger and complex, it begins to show signs of strain, eventually leading to failure. Prominent problems related to procedural approach are as follows:
In real world we come across, deal with and process objects, such as student, employee, invoice, car etc.. Objects are not only data and not only functions, but combination of both.
Each real world object has attributes and behaviour associated with it.

Each attribute will have a value associated with it. Attribute is equivalent to data.
Behaviour is equivalent to function. We can see that in real life, attributes and behaviour are not independent of each other, rather they co-exist.
Most important feature of object oriented approach is defining attributes and their functionality as a single unit called class. It serves as a blueprint for all objects having similar attributes and behaviour. In OOP, class defines what are the attributes its object has, and how is its behaviour. Object, on the other hand is an instance of the class.
Python is a completely object oriented language. Everything in a Python program is an object. A data item created with literal representation represents happens to be an object of respective built-in class.
>>> marks=50
>>> type(marks)
<class 'int'>
>>> percent=55.50
>>> type(percent)
<class 'float'>
>>> name='Jimmy'
>>> type(name)
<class 'str'>
>>> numbers=[12,34,43,21]
>>> type(numbers)
<class 'list'>
You can also define objects of built-in classes using built-in functions of same name (they are actually constructors for respective classes)
>>> x=int(10)
>>> s=str('Hello')
>>> d=dict(one=1, two=2)
A module – either built-in or user-defined – when imported becomes object of module class.
>>> import math
>>> type(math)
<class 'module'>
A built-in function, function in built-in module or a user-defined function is also an object.
>>> type(len)
<class 'builtin_function_or_method'>
>>> type(math.pow)
<class 'builtin_function_or_method'>
>>> def add(x,y):
return x+y
>>> type(add)
<class 'function'>
In addition to built-in classes, class keyword helps in creating a customized class. Following statement defines a class called User.
>>> class User:
'docstring of User class'
pass
As in function or module, first string literal in the block is docstring of the class. We can declare object of this person class as we would for any built-in class.
>>> a=User()
>>> b=User()
Objects (sometimes called instances) of User class do not have any attributes. Instance attributes are defined and initialized in Python class by a special method called constructor.
A method (similar to a function, but inside class) in the class, which is automatically invoked whenever a new object is instantiated, is called constructor. Name of constructor in Python class is __init__(). Let us provide a constructor to User class to define name, email and role as attributes.
>>> class User:
'docstring of User class'
def __init__(self):
self.name='Ram'
self.email='ram@abc.com'
self.role='Manager'
Each method in class must have self as first parameter. It carries reference to calling object. Attributes of calling object are accessed by self.attrib. Let us provide display() method inside User class to display attributes of calling object.
def display(self):
print ('name:',self.name)
print ('email:',self.email)
print ('role:',self.role)
In following Python script, User class is defined.
#user.py
class User:
'docstring of User class'
def __init__(self):
self.name='Ram'
self.email='ram@abc.com'
self.role='Manager'
def display(self):
print ('name:',self.name)
print ('email:',self.email)
print ('role:',self.role)
Let us use above script as a module and import User class. An object a is declared. It automatically calls __init__() constructor to initialize it attributes. Its contents are shown when display() method is called.
>>> from user import User
>>> a=User()
>>> a.display()
name: Ram
email: ram@abc.com
role: Manager
Note that even if more than one objects are declared, each object will be initialized with same values assigned to the attributes. You would ideally want object to be created with different specific values instead of default ones. To achieve this, __init__() method is defined with additional parameters with default values.
#user.py
class User:
def __init__(self, name='Ram', email='ram@abc.com', role='Manager'):
self.name=name
self.email=email
self.role=role
def display(self):
print ('name:',self.name)
print ('email:',self.email)
print ('role:',self.role)
Import User class and declare two objects with different attributes. First one will have default values and second will be initialized with actual arguments provided.
>>> from user import User
>>> x=User()
>>> x.display()
name: Ram
email: ram@abc.com
role: Manager
>>> y=User('Sunil', 'sunil@abc.com', 'admin')
>>> y.display()
name: Sunil
email: sunil@abc.com
role: admin
Traditional object oriented languages like Java use getter and setter methods respectively to return value of and assign value to instance variables. In following Python script (book.py) defines book class having instance method getprice() which returns price attribute of the object, and setprice() which sets price attribute of object with given value.
#book.py
class Book:
def __init__(self, price=40):
self.price=price
def getprice(self):
return self.price
def setprice(self, price):
self.price=price
Following interactive session shows implementation of book class from above script.
>>> from book import Book
>>> b1=Book()
>>> b1.getprice()
40
>>> b1.setprice(55)
>>> b2=Book(100)
>>> b2.getprice()
100
>>> b2.setprice(200)
According to principle of data encapsulation in Object oriented approach, instance attributes of class are available for processing to methods defined within the class only. Methods themselves on the other hand are accessible from outside class context. So object data is hidden from environment that is external to class. Method encapsulates object data so that unwarranted access to it is prevented.
In Java/C++, this mechanism is implemented by use of private and public keywords. Instance attributes are encapsulated data by declaring them as private, whereas methods are given public access.
Python however doesn’t have such mechanism to restrict direct access to instance variables outside the class. It is possible to manipulate price attribute of book’s object without getter/setter method also.
>>> b1.price
55
>>> b1.price=25
Python even has built-in functions getattr() and setattr() to fetch and set values of an instance attribute.
>>> b1=Book()
>>> hasattr(b1, 'price')
True
>>> getattr(b1,'price')
40
>>> setattr(b1, 'price',100)
All instance variables and methods in Python class are public by default. To emulate behaviour of private keyword, instance variable is prefixed by double underscore (__).
Let us modify book.py to make price attribute private.
#book.py
class Book:
def __init__(self, price=40):
self.__price=price
def getprice(self):
return self.__price
def setprice(self, price):
self.__price=price
This ensures that instance variable is accessible only through getter/setter method. If we try to access directly, AttributeError is raised.
>>> b1=Book()
>>> b1.getprice()
40
>>> b1.__price
AttributeError: 'Book' object has no attribute '__price'
However, the double underscore prefix doesn’t really make price truly private (as in case of Java). It merely performs name mangling. Python internally renames private variable by adding the "_ClassName" to front of variable. In this case, variable named "__price" in Book class will be mangled in “_book__price” form.
>>> b1._book__price
40
We can see that private instance variable is still accessible without getter/setter. This is where Python’s built-in property() function is useful.
This function provides an interface to private instance variable. It takes getter, setter and deleter methods as arguments and returns a property object corresponding to an instance attribute of a class.
prop=property(fget, fset, fdel, docstring)
Here fget is the getter method for a certain instance attribute, fset is its setter method and fdel is delete method. The docstring is optional explanatory string. Any type of access to this property automatically calls getter/setter method.
Let us incorporate this concept and introduce price property in Book class as follows :
class Book:
def __init__(self):
self.__price=40
def getprice(self):
print ("getter for price property")
return self.__price
def setprice(self,x):
print ("setter for price property")
self.__price=x
price=property(getprice, setprice, "price")
The private instance variable __price is now having an interface in the form of price as property. It can be accessed directly, but it will internally invoke getprice() or setprice() method.
>>> from book import Book
>>> b1=Book()
>>> b1.price
getter for price property
40
>>> b1.price=100
setter for price property
You can of course use getter and setter methods as before.
>>> b1.getprice()
getter for price property
100
>>> b1.setprice(50)
setter for price property
>>> b1.price
getter for price property
50
Thus Python implements principle of data encapsulation in a different yet effective mechanism of defining property object.