Class and instance attributes

What’s a class attribute?

What’s an instance attribute?

This is a basic Python class with two attributes: class attribute and instance attribute.

  • height and width are an instances attribute defined inside the constructor.
  • number of instances is a class attribute defined outside the constructor.

The differences between class and instance attributes

Advantages of instance attributes

  • They are discarded once the instance is deleted, so they die with the instance they are associated with, which makes things clearer.

Advantages of class attributes

  • They store data that is relevant to all the instances. For example, we could have a counter class attribute that increments every time we create a new instance and decrements every time we delete an instance.

Disadvantages of instance attributes

  • The fact that their values are lost on deletion is also a disadvantage in some cases where you want to keep a history of values for example.

Disadvantages of class attributes

  • If you want to change a class attribute, you have to do it with the notation ClassName.AttributeName. Otherwise, you will create a new instance variable.


The __dict__ of a class however, is not that straight-forward. Its actually an object of a class called dictproxy. dictproxy is a special class whose objects behave like normal dicts, but they differ in some key behaviours.