Classes and Objects in Python 3 | OOP's

Author: neptune | 31st-Aug-2022 | views: 78

We will see the OOPs implementation in Python along with examples.

Introduction to OOP’s

Object-oriented programming can model real-life scenarios and suit the development of large and complex applications.

What is an Object?

In real life, an object is something that you can sense and feel. For example Car, Bicycles, Mango, and more.

However, in Software development, an object is a non-tangible entity, which holds some data and is capable of doing certain things.

What is Class?

A Class is a template that contains:

1. instructions to build an object.

2. methods that can be used by the object to exhibit a specific behaviour.

“class” keyword is used to define a class in Python.


class <ClassName>(<parent1>, ... ):



class Person:


Above example defines Person class without anybody.

How to create Objects?

An object is created by calling the class name followed by a pair of parenthesis.

class Person:             


p1 = Person()      # Creating the object 'p1'


The output of print on object p1, tell you what class it belongs to and hints at the memory address it is referenced to.

Setting Attributes

  • You can set attributes, one at a time, to an instantiated object and access it using the dot notation.

  • The value which is set to an attribute can be anything: a Python primitive, a built-in data type, or another object. It can even be a function or a class.


class Person:


p1 = Person()

p1.fname = 'Jack'

p1.lname = 'Simmons'

>>>print(p1.fname, '-', p1.lname)  # -> 'Jack - Simmons'

1. You can also set multiple attributes, at once, by defining the initializer method, __init__, inside the class.

2. This method is called by default, during an object creation.

3. It takes values passed inside the parenthesis, during an object creation, as it's arguments.

4. It also takes self as the first argument, which refers to the current object.

5. In the following example, the Person class sets two attributes using __init__ method.

class Person:

    def __init__(self, fname, lname):

        self.fname = fname

        self.lname = lname

p1 = Person('George', 'Smith')   

print(p1.fname, '-', p1.lname)           # -> 'George - Smith'

Each class or a method definition can have an optional first line, known as docstring.


class Person:

    'Represents a person.'

    def __init__(self, fname, lname):

        'Initialises two attributes of a person.'

        self.fname = fname

        self.lname = lname

Once documented, you can load the script into an interactive interpreter and run the help command on the Person class.


Help on class Person in module __main__:

class Person(builtins.object)

 |  Represents a person.


 |  Methods defined here:


 |  __init__(self, fname, lname)

 |      Initialises two attributes of a person.

If you have questions, or simply would like to share your thoughts, then feel free to reach out in the comments section below.

Related Blogs
How to extract Speech from Video using Python?
Author: neptune | 29th-Aug-2022 | views: 2347
Simple and easy way to convert video into audio then text using Google Speech Recognition API...
How to download video from youtube using python module ?
Author: neptune | 22nd-May-2022 | views: 1433
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..
Mostly asked Python Interview Questions - 2022.
Author: neptune | 25th-May-2022 | views: 855
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...
Python 3.9 new amazing features ?
Author: neptune | 22nd-May-2022 | views: 739
We are going to explore the newest features of Python 3.9 really amazing features here list of some features like Dict union, Type hinting etc. with examples...
Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 22nd-May-2022 | views: 693
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...
Best Python package manager and package for virtual environment ?
Author: neptune | 15th-Apr-2022 | views: 627
#Python #Anaconda #Virtualenv #Pip
Which is the best package manager for python and Virtual environment management using Virtualenv and Anaconda...
How to reverse string in Python ?
Author: neptune | 16th-May-2022 | views: 512
We are going to explore different ways to reverse string in Python...
Will, AI kills Developer's jobs?
Author: neptune | 22nd-May-2022 | views: 511
#Python #Machine learning #AI
GPT-3’s performance has convinced that Artificial intelligence is closer or at least AI-generated code is closer than we think. It generates imaginative, insightful, deep, and even excellent content...
Do you know Jupyter is now full-fledged IDE?
Author: neptune | 15th-Apr-2022 | views: 426
#Python #Jupyter
Jupyter is a widely used tool by Data scientists. So developers from institutions like Two Sigma, Bloomberg and convert it into IDE lets see..
What exactly you can do with Python?
Author: neptune | 15th-Apr-2022 | views: 373
Well, it's a tricky question to answer because there are lots of application of Python. But, I will tell you about 3 main applications of Python...
Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 22nd-May-2022 | views: 322
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...
Core Python Syllabus for Interviews
Author: neptune | 11th-Jun-2022 | views: 292
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...
Introduction to Apache Spark
Author: neptune | 09th-May-2022 | views: 206
#Python #Apache Spark
Apache Spark is an open-source, distributed processing system that utilizes in-memory caching and optimized query execution for faster queries...
Datatypes in Python.
Author: neptune | 22nd-May-2022 | views: 192
Python have different types of datatypes like Numbers, Strings, Lists, Tuples, Dictionary, Set, Frozenset, Bool, Mutable, and Immutable...
Input and Output in Python
Author: neptune | 15th-Jun-2022 | views: 121
In this article, we will see how Python take input from user and How it display the output to user. First we cover input then output...
Author: neptune | 07th-Jul-2022 | views: 94
In this article, we will explore top 10 Python skills required to get hired in worlds top most companies. The FAANG companies which are Facebook, Amazon, Apple, Netflix, and Google...
Inheritance in Python | OOP's
Author: neptune | 05th-Sep-2022 | views: 25
Inheritance describes is a kind of relationship between two or more classes, abstracting common details into superclass and storing specific ones in the subclass...
View More