Python CLASSES AND OBJECTS’ ATTRIBUTES: everything you need to know

Imene Ayari
2 min readMay 25, 2020

Python is an “object-oriented programming language.” This means that almost all the code is implemented using a special construct called classes.

What is a class?

A class is a code template for creating objects. Objects have member variables and have behavior associated with them. In python a class is created by the keyword class.

How to create a class

The simplest class can be created using the class keyword.

Attributes in class:

A class by itself is of no use unless there is some functionality associated with it. Functionalities are defined by setting attributes:

An attribute of class means these are the terms used for describing the properties or fields or data members or class members of the class. Attribute simply means the data member of an object. In Python, the class attribute is an attribute of the class which belongs to the class itself which means it is the attribute of the class rather than an attribute of an instance of a class. The Python class attributes will be shared by all the instances where instance attributes are not shared between instances. These class attributes are usually defined in the class body parts at the top for better understanding.

An attribute is a changeable property or any characteristics of any component or program or class which can set to different values.

Instance Attribute:

Instance Attributes are unique to each object:

An instance attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object and it is defined inside the constructor function, __init__(self,..) of the class.

objects can also be represented as dictionaries using an inbuilt function called __dict__.

his is the reason, why you can dynamically add attributes to objects of classes that we have created already.

Using a dictionary for attribute storage is very convenient, but it can mean a waste of space for objects, which have only a small amount of instance variables

While an instance attribute defined inside the constructor a class attribute defined outside of it.

The class attribute is accessible as both a property of the class and as a property of objects, as it is shared between all of them.

All of the above can be really used with caution in order to avoid unexpected behavior.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet