Class or Static Variables in Python

Python Class or Static Variables: In this tutorial, we will learn about the class or static variables in Python with the help of examples. Also learn the features, advantages, and disadvantages of class/static variables. By IncludeHelp Last updated : June 26, 2023

What are class or static variables in Python?

Class or static variables are class-related variables that are shared among all objects but the instance or non-static variable is unique for each object. Static variables are created inside the class but outside of the methods and they can be accessed through the class.

In Python, there is no need for the keyword static to make a class variable but the static keyword is used to make a class or static variables in other programming languages like C++, C, Java, etc.

Features of static variables

  • Static variables are class variables thus they are created inside the class but outside of the methods.
  • Static variables are accessed through the class.
  • The behavior of the static variables is fixed and doesn't change with every object.
  • Since static variables are class variables thus the memory is allocated once for the static variable. They don't take the memory with each instance of the class.
  • All instances of the class share the same copy of the static variables.

Static variables creation

Class or static variables are created inside the class definition but outside of the methods.

Syntax

Consider the below syntax to create a static variable -

class class_name:
    static_variable_1 = value
    static_variable_2 = value
    ...
    ...
	
    # Class methods definitions 
    ...
    ...

Accessing static variables

Static variables can be accessed with the class name without creating its instance, they can also be accessed with the instance name if you created it.

Consider the below example -

Example

# class definition
class intern:
    site_name = "Includehelp"
    field = "Technical content writer"

    def __init__(self, name, programming_language):
        self.name = name
        self.programming_language = programming_language

# Main code
# Without creating instance
print("Accessing using class name...")
print(intern.site_name)
print(intern.field)

# Using instance
std = intern("ALex", "Golang")
print("\nAccessing using instance name...")
print(std.site_name)
print(std.field)

Output

Accessing using class name...
Includehelp
Technical content writer

Accessing using instance name...
Includehelp
Technical content writer

Modifying static variables

There are two different ways to modify the values of static variables: using the class name and using its instance. But you must use the class name to modify the static variables as it reflects updated value across all class instances.

Consider the below example -

Example

# class definition
class intern:
    site_name = "Includehelp"
    field = "Technical content writer"

    def __init__(self, name, programming_language):
        self.name = name
        self.programming_language = programming_language

# Main code
print("Original values...")
print(intern.site_name)
print(intern.field)

# Modifying static variables
intern.site_name = "Wikipedia.org"
intern.field = "Analyst"

print("\nUpdated values...")
print(intern.site_name)
print(intern.field)

Output

Original values...
Includehelp
Technical content writer

Updated values...
Wikipedia.org
Analyst

Example of class or static variables

Let's look at an example for a better understanding of the static variable that returns some details about the intern of "includehelp" in Python.

# class definition
class intern:
    site_name = "Includehelp"
    field = "Technical content writer"

    def __init__(self, name, programming_language):
        self.name = name
        self.programming_language = programming_language

# Main Code
# objects of the intern class
std1 = intern("Bipin Kumar", "Python")
std2 = intern("Shivang Yadav", "C++")

# printing the variables values
print("Site name: ", std1.site_name)
print("Field of interest: ", std1.field)
print("Name of intern: ", std1.name)
print("Language: ", std1.programming_language)
print()

print("Site name: ", std2.site_name)
print("Field of interest: ", std2.field)
print("Name of intern: ", std2.name)
print("Language: ", std2.programming_language)

Output

Site name:  Includehelp
Field of interest:  Technical content writer
Name of intern:  Bipin Kumar
Language:  Python

Site name:  Includehelp
Field of interest:  Technical content writer
Name of intern:  Shivang Yadav
Language:  C++

Explanation:

In the above example, site_name and field are the class or static variables which are declared inside the class intern but outside of the __init__ method. name and programming_language are instance variables. Here, we declared two objects std1 and std2 of the intern class by providing the values instance variables and the static variables are common for both objects.

Advantages of static variables

  • Static variables are memory efficient. The memory for the static variable is created once and each instance of the class shares the same memory location.
  • Static variables are useful to maintain the shared state. Since static variables are class variables and work through the class. Thus, they are very useful to maintain the shared state across all instances.
  • Static variables are easy to access.
  • Static variables are helpful to increase the readability of the code.
  • Static variables are useful to unitize class-related variables. Since they are initialized once in the class, it is flexible to initialize and modify the data.

Disadvantages of static variables

  • Static variables have limited scope. They cannot be accessed outside of the class in which they are declared.
  • The values of the static variables are fixed for all instances of the class and you cannot change their values based on the different instances. This may cause a lack of flexibility.
  • Static variables may cause thread safety concerns when working with multi-threading.

FAQs

These are some of the popular frequently asked questions related to Python static/class variables.

What's the difference between a static variable and an instance variable?

The static variables are the class variables and they share the same memory among all class instances while instance variables are the data members of the class and they share a separate copy for each class instance.

Can you access static variables without declaring an instance of the class?

Yes, you can. The static variables are the class variable and can be accessed directly using the class name without declaring an instance of the class.

What happens if you modify the static variables using the class instance?

The static variables must be modified using the class name. But, if you modify them using the instance the static variables will become instance variables with the same name and updated values will not reflect across all instances.

Can static variables be inherited by subclasses?

Yes! During the inheritance the static variables can be inherited by the subclass, they must be defined inside the parent class.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.