Python Class Variables With Examples

In an object-oriented programming language, instance and class variables are used while defining the class. A class variable in Python is a variable that is shared/common among all the class instances. Instance variable values vary from object to object, while class variable values are declared inside the class but outside any method.

This blog post demonstrates the in-depth guide on Python class variables using multiple examples. This post will explain the following contents:

How to Create Class Variables in Python?

To create class variables, we need to initialize inside the main class before other methods, such as the “__init__()” constructor or instance method. Here is an example code:

Code:

class Team:
    # Class variable
    team_name = 'Falcons'
   
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no

s2 = Team('Joseph', 958)
print(Team.team_name)
  • The class named “Team” is defined in the program.
  • The class variable named “team_name” is defined before the initialization of the “__init__()” constructor.
  • The class variable is accessed using the class name “Team”.

Output:

The above snippet shows the value of class variables.

How to Access Class Variables in Python?

To access the class variables, different methods are used in Python, such as object references, class names, etc.

Example 1: Using Constructor Self Parameter

The below code uses the “self parameter” to access the class variable:

Code:

class Team:
    # Class variable
    team_name = 'Falcons'
   
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no
        print(self.team_name)

s2 = Team('Joseph', 958)
  • The class named “Team” and the class variable “team_name” is initialized in the program.
  • To access the class variable, the “self” instance of the “__init__()” constructor is used inside the constructor, such as “self.team_name”.

Output:

The class variable’s value has been successfully returned.

Example 2: Using Class Name

The below code utilizes the “class name” to access/call the class variable:

Code:

class Team:
    # Class variable
    team_name = 'Falcons'
   
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no
        print(Team.team_name)

s2 = Team('Joseph', 958)
  • The class named “Team” and the class variable “team_name” is initialized in the program.
  • The class name is also used inside the “__init__()” constructor to access the class variable value, such as “Team.team_name”.

Output:

The class variable value has been successfully accessed.

Example 3: Accessing Class Variable in Instance Method

The below code uses the “self” parameter to access the class variable in the instance method:

Code:

class Team:
    # Class variable
    team_name = 'Falcons'
   
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no
    def players(self):
        print('Instance Method: ')
        print(self.team_name)
 
s2 = Team('Joseph', 958)
s2.players()
  • The “__init__()” constructor and instance methods “players()” are defined in the class.
  • In the “instance method”, the class variables are accessed using the “self” parameter such as “self.team_name”.
  • We can also access the class variables using the class name.

Output:

The above output shows the class variable’s value when accessed inside the instance method.

How to Use Class Variables in Inheritance?

The inheritance class has all properties and attributes of the main class. In the below code, the “class variables” of the main class are accessed in the sub or child class:

Code:

class Team:
    # Class variable
    team_name = 'Falcons'

class Players(Team):  
   
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no
   
    def get_players(self):
        print(self.team_name)
        Players.team_name = 'Strikers'
        print(self.team_name)
 
s2 = Players('Joseph', 958)
s2.get_players()
  • The main class named “Team” and the subclass named “Players” are defined in the program.
  • The class variable of the main class is also accessible in the child class, so the value is modified and accessed using the self parameter.
  • We can also access the new value of a class variable using the sub-class name “Players” in the instance method of the subclass.

Output:

The above snippet shows the class variable value before and after the modification in the inheritance class method. 

Conclusion

The class variable is defined inside the class but outside the method of the class, such as the “__init__()” constructor and “instance method”. Various methods are used in Python to access the class variable, such as using the constructor self parameter, using the class name, and accessing a class variable in an instance method. The class variables can also be accessed and modified in the inheritance class in Python. This tutorial presented a thorough guide on class variables using numerous examples.