OOP basics: Class Vs Instance attributes in Python3

Frank
Frank
May 28, 2019 · 3 min read
OOP in Python3

Definition of attributes

In Object-oriented programming(OOP), classes and objects have attributes. Attributes are data stored inside a class or instance and represent the state or quality of the class or instance. In short, attributes store information about the instance. Also, attributes should not be confused with class functions also known as methods. One can think of attributes as noun or adjective, while methods are the verb of the class.

Class attributes

These attributes belong to the class itself, you can call them class variables. Example:

In the above snippet, attributeis a class attribute and you can access using:

or

They both will print “This is an attribute of a class”. No matter how many times you create instances of the class, they will all have a copy ofattribute . Remember, it will be copied not shared:

As you can see on the example, even though we are changing the variable via the instance to Changed the value string, when you access it using the class name NewClass.attribute the value has not been changed

Instance attributes

Instances or objects can have their own attributes. The easiest way to create an instance attribute is just assigning a new value to a new variable using instance name and . :

The new instance attribute will be created, but you cannot access it using the class name, AttributeError will be thrown as an error.

Difference between class and instance attributes

Both class and instance attribute differences can be explained using __dict__ attribute and the term namespace. A namespace is a mapping from names to objects, with the property that there is zero relation between names in different namespaces. They’re usually implemented as Python dictionaries. Python classes and instances of classes each have their own distinct namespaces. When you try to access an attribute, python will loop for that attribute inside local namespace:

As you can see here, the local namespace is empty, but still, it's printing attibute variable because python interpreter will search for that attribute one in a parent class and so on. This means that local namespace will take precedence and you can overwrite class attributes:

In this example, we are creating a variable called attribute which belongs to object but have the same name as the class attribute. Using __class__ you can still have access to class attributes.

Advantages of class and instance attributes

Class attributes are mainly used to store information which is relevant across all instances.

In this example, number class attribute will act as a global counter which will count the number of instances of the Animal class. The benefit is you will know how many instances have been created by accessing number from any instance.

While class attributes act as a global shared variable, instance attributes will store data about a specific instance, be it breed, fur color or name in case of dog instance, which is unique to each instance.

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Frank

Written by

Frank

Software engineer student. I am passionate about AI and believe that it will change the world

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Frank

Written by

Frank

Software engineer student. I am passionate about AI and believe that it will change the world

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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