Python — How object and class attributes work ?

A class is a collection of variables, known as attributes, and functions, known as methods. A variable of type C is designated by an instance of the class C. (the term instance applies only to variables whose type is a class).

Declaration of a class :

class name_of_the_class :

# body of the class

# …

What is a class attribute ?

A class attribute is a variable that belongs to a class rather than to a particular object. It is shared between all the objects of this class and is defined outside the constructor function, __init__(self,…), of the class.

In the image above, we can see the definition of a class named A and its class attribute a. We can access a class attribute via an instance or via the class name directly.

Generally, we define class attributes outside all the methods and they are placed at the top, right below the class header.

If a class attribute is updated then the new instances of the created object will have a new default value.

What is an instance attribute ?

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

In the example above, we have a class Dog which have a class attribute: species and two instance attributes: name and age.

The instance attributes are created inside the constructor __init__. This method is automatically called after an instance of a class has been created. It’s usually the first method defined inside a class. The first argument of __init__ ,self ,refers to the current instance of the class.

What are the differences between class and instance attributes ?

There are significant distinctions between class and instance characteristics, as we saw in the previous example.

To begin with, they are not defined in the same place in a class declaration; class attributes are defined inside the constructor, whereas instance attributes are defined outside of all methods.

Class attributes are thus available to all instances of the class, whereas instance attributes are only available to one instance of the class.

A class attribute can also be accessible via an instance or the class name, whereas an instance attribute can only be accessed via the instance to which it belongs.

What are the advantages and drawbacks of each of them ?

Advantages of class attributes:

  • They store data that is relevant to all the instances, like a counter for example.

Disadvantages of class attributes:

  • All your instances will have the same value so you cannot use them to do different things on different objects.

Advantages of instances attributes:

  • They are easy to set and get thanks to properties.
  • Their values are lost on deletion so it makes things clearer.

Disadvantages of instances attributes:

  • It is not possible to keep track of the values between instances.

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

If you want to check all of your class attributes, you can use the special method __dict__ to list them.



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