Tutorial

Understanding Class and Instance Variables in Python 3

Updated on August 20, 2021
Understanding Class and Instance Variables in Python 3

Introduction

Object-oriented programming allows for variables to be used at the class level or the instance level. Variables are essentially symbols that stand in for a value you’re using in a program.

At the class level, variables are referred to as class variables, whereas variables at the instance level are called instance variables.

When we expect variables are going to be consistent across instances, or when we would like to initialize a variable, we can define that variable at the class level. When we anticipate the variables will change significantly across instances, we can define them at the instance level.

One of the principles of software development is the DRY principle, which stands for don’t repeat yourself. This principle is geared towards limiting repetition within code, and object-oriented programming adheres to the DRY principle as it reduces redundancy.

This tutorial will demonstrate the use of both class and instance variables in object-oriented programming within Python.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Class Variables

Class variables are defined within the class construction. Because they are owned by the class itself, class variables are shared by all instances of the class. They therefore will generally have the same value for every instance unless you are using the class variable to initialize a variable.

Defined outside of all the methods, class variables are, by convention, typically placed right below the class header and before the constructor method and other methods.

Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command. Then you can copy, paste, or edit the examples by adding them after the >>> prompt.

A class variable alone looks like the following:

class Shark:
    animal_type = "fish"

Here, the variable animal_type is assigned the value "fish".

We can create an instance of the Shark class (we’ll call it new_shark) and print the variable by using dot notation:

shark.py
class Shark:
    animal_type = "fish"

new_shark = Shark()
print(new_shark.animal_type)

Let’s run the program:

  1. python shark.py
Output
fish

Our program returns the value of the variable.

Let’s add a few more class variables and print them out:

shark.py
class Shark:
    animal_type = "fish"
    location = "ocean"
    followers = 5

new_shark = Shark()
print(new_shark.animal_type)
print(new_shark.location)
print(new_shark.followers)

Just like with any other variable, class variables can consist of any data type available to us in Python. In this program we have strings and an integer. Let’s run the program again with the python shark.py command and review the output:

Output
fish ocean 5

The instance of new_shark is able to access all the class variables and print them out when we run the program.

Class variables allow us to define variables upon constructing the class. These variables and their associated values are then accessible to each instance of the class.

Instance Variables

Instance variables are owned by instances of the class. This means that for each object or instance of a class, the instance variables are different.

Unlike class variables, instance variables are defined within methods.

In the Shark class example below, name and age are instance variables:

class Shark:
    def __init__(self, name, age):
        self.name = name
        self.age = age

When we create a Shark object, we will have to define these variables, which are passed as parameters within the constructor method or another method.

class Shark:
    def __init__(self, name, age):
        self.name = name
        self.age = age

new_shark = Shark("Sammy", 5)

As with class variables, we can similarly call to print instance variables:

shark.py
class Shark:
    def __init__(self, name, age):
        self.name = name
        self.age = age

new_shark = Shark("Sammy", 5)
print(new_shark.name)
print(new_shark.age)

When we run the program above with python shark.py, we’ll receive the following output:

Output
Sammy 5

The output we receive is made up of the values of the variables that we initialized for the object instance of new_shark.

Let’s create another object of the Shark class called stevie:

shark.py
class Shark:
    def __init__(self, name, age):
        self.name = name
        self.age = age

new_shark = Shark("Sammy", 5)
print(new_shark.name)
print(new_shark.age)

stevie = Shark("Stevie", 8)
print(stevie.name)
print(stevie.age)
Output
Sammy 5 Stevie 8

The stevie object, like the new_shark object passes the parameters specific for that instance of the Shark class to assign values to the instance variables.

Instance variables, owned by objects of the class, allow for each object or instance to have different values assigned to those variables.

Working with Class and Instance Variables Together

Class variables and instance variables will often be utilized at the same time, so let’s look at an example of this using the Shark class we created. The comments in the program outline each step of the process.

shark.py
class Shark:

    # Class variables
    animal_type = "fish"
    location = "ocean"

    # Constructor method with instance variables name and age
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Method with instance variable followers
    def set_followers(self, followers):
        print("This user has " + str(followers) + " followers")


def main():
    # First object, set up instance variables of constructor method
    sammy = Shark("Sammy", 5)

    # Print out instance variable name
    print(sammy.name)

    # Print out class variable location
    print(sammy.location)

    # Second object
    stevie = Shark("Stevie", 8)

    # Print out instance variable name
    print(stevie.name)

    # Use set_followers method and pass followers instance variable
    stevie.set_followers(77)

    # Print out class variable animal_type
    print(stevie.animal_type)

if __name__ == "__main__":
    main()

When we run the program with python shark.py, we’ll receive the following output:

Output
Sammy ocean Stevie This user has 77 followers fish

Here, we have made use of both class and instance variables in two objects of the Shark class, sammy and stevie.

Conclusion

In object-oriented programming, variables at the class level are referred to as class variables, whereas variables at the object level are called instance variables.

This differentiation allows us to use class variables to initialize objects with a specific value assigned to variables, and use different variables for each object with instance variables.

Making use of class- and instance-specific variables can ensure that our code adheres to the DRY principle to reduce repetition within code.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


Tutorial Series: How To Code in Python

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Tutorial Series: Object-Oriented Programming in Python 3

Object-oriented programming (OOP) focuses on creating reusable patterns of code, in contrast to procedural programming, which focuses on explicit sequenced instructions. When working on complex programs in particular, object-oriented programming lets you reuse code and write code that is more readable, which in turn makes it more maintainable.

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Nice article, but I think it still needs some love:

Even if set_follwers() is a method, it still does not set an instance variable. The value of the method parameter (or method argument) followers is not saved anywhere in the instance. As soon as the method finishes, the method local variables and arguments cease to exist. To actually set an instance variable you need to add the statement

    self.followers = followers

inside the function set_followers(). Then it will be possible to do this in main():

    # Use set_followers method and pass followers instance variable
    stevie.set_followers(77)           # As before... but now also:
    print("Shark 'Stevie' has " + str(stevie.followers) + " followers")

Another aspect is that I believe it is poor practice to add new instance variables in methods other than __init__() because that means different instances have different sets of instance variables. To avoid this, add to the __init__() method:

    self.followers = 0

Thanks for the article. It cleared the basic points of Variables, Instances and Methods.

I still don’t understand why there are two kinds of variables. Is there anything instance variable can do which class variable can’t and vice versa?

I have been only using the instance variable in my career until now.

Thanks for the article! In function set_followers() you don’t use any of class/instance variables, you use function parameter only. pylint would say that it could be a function, not a method. And it is mostly print_followers().

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more