Object and class attributes in python

Joudirim
3 min readMay 24, 2021

--

Python is an object oriented programming language, almost everything in Python is an object, with its properties and methods.

In this blog we are going to describe how object and class attributes work.

So first we need to ask some questions that we are going to answer later on such as:

  • What’s a class attribute?
  • What’s an instance attribute?
  • What are all the ways to create them and what is the Pythonic way of doing it?
  • What are the differences between class and instance attributes?
  • What are the advantages and drawbacks of each of them and how does Python deal with the object and class attributes using the __dict__.

But before starting we need to know what is OOP?

Object-Oriented Programing It is a programming paradigm (which is a basic model of the project and program design) that is based on 4 principal pillars which are abstraction, encapsulation, inheritance, and polymorphism. That helps us to abstract objects from the real world.

What’s a class attribute?

Class attributes are variables of a class that are shared between all of its instances. They differ from instance attributes in that instance attributes are owned by one specific instance of the class only, and ​are not shared between instances.

What’s an instance attribute?

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

What are all the ways to create Classes and Instance

Creating Classes and Instance could be in a non-Pythonic way and in a Pythonic way;

  • For a non-Pythonic way is like the following:
class Square:def __init__(self, size=0):     
if not isinstance(size, int):
raise TypeError("size must be an integer")

if size < 0:
raise ValueError("size must be >= 0")
self.__size = size * size

def area(self):
return self.__size
  • For the pythonic way is like the following:
class Square:
def __init__(self, size=0):
self.size = size

@property
def size(self):
return self.__size

@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
if value < 0:
raise ValueError("size must be >= 0")
self.__size = value

def area(self):
return self.__size * self.__size

What are the advantages and drawbacks of each of them?

The disadvantage comes when we need to modify the code.

The pythonic way of doing it would be to use getter and setter property methods.

The advantage of using property allows us to attach code to the self.size attribute and any code assigned the value of size will be called with size in def size.

Differences Between Class and Instance Attributes

The difference is that class attributes is shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. The attribute of an instance on the other hand is unique to that instance.

How does Python deal with the object and class attributes using the __dict__

__dict__ is a method that returns a dictionary with all the attributes of a class or an instance of a class. So each instance is stored in a dictionary, so it retrives each instance and its class.

The __dict__ is used in this way:

object.__dict__

Let’s see how it works;

class MyClass(object):
x = 1
def __init__(self, x):
self.x = x
MyClass1 = MyClass(2)
MyClass2 = MyClass(3)
print MyClass1.__dict__
print MyClass2.__dict__

This gives the output

{'x': 2}
{'x': 3}

Enjoy reading!

--

--

Joudirim
Joudirim

Written by Joudirim

Holbertonschool software engineering student

No responses yet