Delving into classes and objects in python

what are classes and why do we need them?

Classes provide a means of grouping data and functionality together in a program. but it’s not strictly necessary to have classes in python, as it supports functions, but since python was designed keeping OOP(object-oriented programming) in mind, we encounter classes. A class is like a blueprint for an object.

Class definition syntax :

we use the word class followed by a user-defined name for the class while defining it and a semicolon.

Class Objects

An Object is an instance of a Class. what that essentially means is, In object-oriented programming (OOP), an instance is a concrete occurrence of any object, existing usually during the runtime of a computer program.

An object represents:

  • State: It is represented by the attributes of an object.
  • Behavior: It is represented by the methods of an object.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.
image by Daksh-Deepak\medium

Creating an object — also called instantiation

You can access the elements of the class with the object using the dot operator.

The — init — () function

All classes have a function called __init__(), which is always executed when the class is being’s used to assign values to object properties and variables. for example :

The Pass parameter

Class definitions cannot be empty, but if you for some reason have a class definition with no content put in the pass statement to avoid getting an error. as shown earlier.

The self parameter

The self parameter refers to the current instance of the class and is used to access variables that belong to the class. here's an example:


A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory. Even when a function is invoked and is out of scope, a closure allows the function to access those captured variables through the closure’s copies of their values or references.



Functions in Python are first-class citizens. A decorator is a design pattern in Python that allows a user to add new functionality to an existing object while maintaining its structure intact. Decorators are usually called before the definition of a function you want to decorate.

In the above code, ABC_decorator is a callable function, will add some code on the top of another callable function, hello_decorator function, and return the wrapper function.

pic by TechBeamers

Descriptors and Properties

Descriptors are Python objects that implement a method of the descriptor protocol, hence giving you the ability to create objects that have special behavior when they’re accessed as attributes of other objects.

Defining a definition protocol :

If your descriptor implements just -get — , then it’s said to be a non-data descriptor. If it implements .__set__() or .__delete__(), then it’s said to be a data descriptor.

The property() function is used to define properties in the Python class. The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides property.

we’ll explore more about properties in the upcoming posts!




A curious learner , an engineer by profession , exploring the world through writings

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

4 Simple Steps To Check The Metrics Setup Of Your A/B Tests

Setting up security scans for container images

5 Tips for Reducing Bugs in Software Development Projects

Displays with code

Calculating the End-to-End distance of two atoms using MDAnalysis


Universal HLS playback in 2021



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditi Pateriya

Aditi Pateriya

A curious learner , an engineer by profession , exploring the world through writings

More from Medium

Top 5 Python Functions to code like a pro

Here’s how to create your first desktop application in python

Java, in case you hadn’t heard

Boolean Operators in Python