0% found this document useful (0 votes)
4 views

Python Notes5

The document discusses Object-Oriented Programming (OOP) in Python, highlighting its importance in organizing software design around data and objects. It outlines the four main concepts of OOP: inheritance, encapsulation, polymorphism, and data abstraction, and provides eight tips for effective OOP practices in Python. These tips include using meaningful names, understanding class vs instance data, and adhering to PEP 8 guidelines for code quality.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Notes5

The document discusses Object-Oriented Programming (OOP) in Python, highlighting its importance in organizing software design around data and objects. It outlines the four main concepts of OOP: inheritance, encapsulation, polymorphism, and data abstraction, and provides eight tips for effective OOP practices in Python. These tips include using meaningful names, understanding class vs instance data, and adhering to PEP 8 guidelines for code quality.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Skip to content

geeksforgeeks

Courses
Tutorials
Practice
Contests

Sign In

Python Course
Python Tutorial
Interview Questions
Python Quiz
Python Glossary
Python Projects
Practice Python
Data Science With Python
Python Web Dev
DSA with Python
Python OOPs

Explore GfG Courses


Share Your Experiences
Python OOPs Concepts
Python Classes and Objects
Python objects
Class and Object
Encapsulation and Access Modifiers
Inheritance
Polymorphism
Abstraction
Special Methods and Testing
Additional Resources
Object oriented testing in Python
classmethod() in Python
Decorators in Python
Destructors in Python
8 Tips For Object-Oriented Programming in Python
DSA to Development Course

8 Tips For Object-Oriented Programming in Python


Last Updated : 08 Mar, 2022

OOP or Object-Oriented Programming is a programming paradigm that organizes


software design around data or objects and relies on the concept of classes and
objects, rather than functions and logic. Object-oriented programming ensures code
reusability and prevents Redundancy, and hence has become very popular even in the
fields outside software engineering like Machine Learning, Artificial Intelligence,
Data Science, etc. There are many object-oriented programming languages like Java,
JavaScript, C++, Python, and many others.
8-Tips-For-Object-Oriented-Programming-in-Python
Basics of Object-Oriented Programming in Python

As stated earlier, OOP is a programming paradigm that uses objects and classes and
aims to implement real-world entities in programming. The main concepts of OOP are
stated below:
1. Inheritance: It is the process in which one class inherits the methods and
attributes from another class. The class whose properties and methods are inherited
is known as the Parent class and the class which inherits from the parent class is
called the Child class. Inheritance is the most important aspect of object-oriented
programming and provides the reusability of the code.

2. Encapsulation: The word, “encapsulate” means to enclose something and the


principle of encapsulation works in the same way. In OOP, the data and functions
which operate on that data are wrapped together into a single unit by
encapsulation. By this method, we can hide private details of a class and can only
expose the functionality that is needed for interfacing with it.

3. Polymorphism: Polymorphism is taken from the Greek words Poly and morph which
means many and shape respectively. Hence in OOP, Polymorphism refers to the
functions having the same names but having different functionalities. Polymorphism
helps in making programming more intuitive and easier.

4. Data Abstraction: Abstraction is another functionality of OOP in which we hide


the internal details or implementations of a function and show the functionalities
only. In other words, the users know "what the function does" but they don't know
"how it does" because they only get to see the basic implementation of the
function, whereas the inner working is hidden.

To know more about the 4 pillars of Object-oriented programming click here. In


this blog, we will discuss the 8 Tips for Object-Oriented Programming in Python. So
Let’s take a look.
1. Difference of Class and Instance Level Data

To under inheritance, learning to differentiate between class-level and instance-


level data is very crucial, as inheritance is one of the core concepts of OOP. An
instance attribute is a Python variable belonging to one object and is unique to
it. It is accessible only in the scope of this object and is defined inside the
constructor function of the class. Whereas, a class attribute is unique to a class
rather than a particular object and is defined outside the constructor function.
While writing a code, Instance-level data should be kept separate from class-level
data and should not interfere with it.
2. Using Meaningful Names

One of the best practices to follow while writing Object-Oriented Programming is to


use meaningful names keeping in mind that the naming convention must follow camel
casing. There are rules which apply while naming attributes and functions as well.
Hence, we should always name the design in such a way that one class, attribute, or
function is responsible for one particular task only.
3. Knowing the Use of Static

Static means that the member is bound to a class level rather than to an instance
level. A static method can not access or modify the state of the class. By using
static, the method doesn't need access to the class instance. Also, using static
methods improves code readability and saves a lot of memory.
4. Deciding Between Internal and Private Modifiers

Access specifiers in Python play an important role in securing data from


unauthorized access and from being exploited. All data members and member functions
of a class are declared public by default whereas to declare an internal method or
attribute, a leading underscore '_' is used. On the other hand, to declare a
private one a double leading underscore '__' is used. This practice helps in
determining the access control of a specific data member or a member function of a
class and tells the users which parts of the code are not public and should not be
relied on.
5. Docstrings in Python

Developers are used to writing comments in the code but comments do not always
provide the most structured way of workflows. For this inconvenience, Python
structured documentation or docstrings provide a convenient way of associating
documentation with Python public modules, functions, classes, methods, etc. Unlike
source code comments, the docstring describes what the function does and not how.
Docstrings in python are declared using '''triple single quotes''' or """triple
double quotes""".
6. pep8 Guidelines

Python Enterprise Proposal also called PEP are coding conventions for python
packages. Besides writing a code with proper logic, many other important factors
affect a code's quality. PEP 8 is a document that was written in 2001 by Guido van
Rossum, Barry Warsaw, and Nick Coghlan with its main aim to enhance the readability
and consistency of code. One should keep in mind three important pep8 guidelines
that are, to ensure that each line of code is limited to 80 characters, all
libraries should be imported at the beginning, and to eliminate redundant variables
or intermediary variables present in the code.
7. Setting Access to Attributes

Attributes of a class are function objects that are used to implement access
controls of the classes and define corresponding methods of its instances. To
access and manipulate the attributes of a class, python has some in-built methods.
Those are getattr(), hasattr(), setattr() and delattr(). The getattr() function
helps in accessing the attribute of the object. The hasattr() function is used in
checking if an attribute exists or not. Setattr() is used to set an attribute. In
case the attribute does not exist, then one would be created, whereas, delattr() is
used to delete an attribute.
8. Using Abstract Classes

An abstract class provides a common interface for different implementations of a


component. In object-oriented programming, developers need to reduce the amount of
coding, and hence, abstract classes are used. An abstract class is used as it
applies to a wide variety of objects and helps in the creation of a set of methods
that must be created within any child classes built from that abstract class.

Similar Reads
R - Object Oriented Programming
In this article, we will discuss Object-Oriented Programming (OOPs) in R
Programming Language. We will discuss the S3 and S4 classes, the inheritance in
these classes, and...
15+ min read
Top 10 Object-Oriented Programming Languages in 2024
In the present world, almost all sectors make use of software applications or
computer programs that require to be coded. Programming Language is a set of
instructions use...
15+ min read
A Step-by-Step Approach to Learn Object Oriented Programming
Object-Oriented Programming or OOP's refers to the language that uses objects in
programming. It is primarily concerned with the implementation of real-world
entities such...
15+ min read
Last Minute Notes (LMNs) - Python Programming
Python is a widely-used programming language, celebrated for its simplicity,
comprehensive features, and extensive library support. This "Last Minute Notes"
article aims t...
15+ min read
Metaprogramming with Metaclasses in Python
Metaprogramming in Python lets us write code that can modify or generate other code
at runtime. One of the key tools for achieving this is metaclasses, which allow us
to c...
15+ min read
How to Create a Programming Language using Python?
In this article, we are going to learn how to create your own programming language
using SLY(Sly Lex Yacc) and Python. Before we dig deeper into this topic, it is to
be no...
15+ min read
Programming Paradigms in Python
Paradigm can also be termed as a method to solve some problems or do some tasks. A
programming paradigm is an approach to solve the problem using some programming
language...
15+ min read
Print Objects of a Class in Python
In object-oriented programming (OOP), an object is an instance of a class. A class
serves as a blueprint, defining the structure and behavior of objects, while an
instance...
15+ min read
10 Best Beginner's Tips for Learning Python
Python is a high-level, interpreted, general-purpose programming language that
supports both object-oriented programming and structured programming. It is quite
versatile...
15+ min read
Getting Started with Competitive Programming in Python
Python is a great option for programming in Competitive Programming. First off, its
easy-to-understand and concise grammar enables quicker development and simpler
debuggin...
15+ min read
course-img
53k+ interested Geeks
Complete Data Analytics Program
course-img
89k+ interested Geeks
DSA for Interview Preparation
course-img
278k+ interested Geeks
Python Full Course Online - Complete Beginner to Advanced
course-img
2k+ interested Geeks
Complete Python Program - Mastering from Fundamentals to Advanced Concepts
course-img
330k+ interested Geeks
Data Structures & Algorithms in Python - Self Paced
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar,
Uttar Pradesh, 201305
GFG App on Play Store
GFG App on App Store
Advertise with us

Company
About Us
Legal
Privacy Policy
Careers
In Media
Contact Us
GfG Corporate Solution
Placement Training Program

Explore
Job-A-Thon Hiring Challenge
GfG Weekly Contest
Offline Classroom Program
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos

Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial

DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming

Data Science & ML


Data Science With Python
Data Science For Beginner
Machine Learning
ML Maths
Data Visualisation
Pandas
NumPy
NLP
Deep Learning

Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS

Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question

Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths

DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
DevOps Roadmap

System Design
High Level Design
Low Level Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
System Design Bootcamp
Interview Questions

School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar

Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB

Preparation Corner
Company-Wise Recruitment Process
Aptitude Preparation
Puzzles
Company-Wise Preparation

More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets

Machine Learning/Data Science


Complete Machine Learning & Data Science Program - [LIVE]
Data Analytics Training using Excel, SQL, Python & PowerBI - [LIVE]
Data Science Training Program - [LIVE]
Data Science Course with IBM Certification

Programming Languages
C Programming with Data Structures
C++ Programming Course
Java Programming Course
Python Full Course

Clouds/Devops
DevOps Engineering
AWS Solutions Architect Certification
Salesforce Certified Administrator Course

GATE 2026
GATE CS Rank Booster
GATE DA Rank Booster
GATE CS & IT Course - 2026
GATE DA Course 2026
GATE Rank Predictor

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy
& Privacy Policy
Lightbox

You might also like