Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Static Class variables in Python

In Python, static class variables, also known as class-level variables or class attributes, are variables that are shared among all instances of a class. These variables are defined directly within the class, outside any instance methods, and are not tied to any particular instance of the class.

Here's an example of a static class variable:

class MyClass:
    static_variable = 42

    def instance_method(self):
        print("Instance method called")


# Access the static variable through the class itself
print(MyClass.static_variable)  # Output: 42

# Create instances of MyClass
instance1 = MyClass()
instance2 = MyClass()

# Access the static variable through instances of the class
print(instance1.static_variable)  # Output: 42
print(instance2.static_variable)  # Output: 42

# Modify the static variable through the class
MyClass.static_variable = 84

# The changes are reflected in all instances
print(instance1.static_variable)  # Output: 84
print(instance2.static_variable)  # Output: 84

In this example, the static_variable is a static class variable that is shared among all instances of MyClass. When the static variable is accessed or modified through the class or any instance, the changes are reflected in all instances of the class.

Keep in mind that if you assign a new value to the static variable through an instance, you'll create an instance variable with the same name, effectively "shadowing" the class variable.

# Assign a new value to the static variable through an instance
instance1.static_variable = 100

# Now instance1 has an instance variable with the same name
print(instance1.static_variable)  # Output: 100

# The class variable and other instances are not affected
print(MyClass.static_variable)  # Output: 84
print(instance2.static_variable)  # Output: 84

In this case, instance1 has an instance variable named static_variable, which does not affect the class variable or other instances.

  1. Static variables in Python classes:

    • Description: Static variables, also known as class variables, are shared among all instances of a class. They are defined at the class level and accessed using the class name.
    • Example Code:
      class MyClass:
          static_variable = 42
      
      # Accessing static variable
      print(MyClass.static_variable)
      
  2. Class-level variables in Python:

    • Description: Class-level variables are variables defined at the class level and shared among all instances of that class.
    • Example Code:
      class MyClass:
          class_variable = "Class-level variable"
      
      # Accessing class-level variable
      print(MyClass.class_variable)
      
  3. Python class attributes and static variables:

    • Description: Class attributes can include static variables, providing shared data among all instances of the class.
    • Example Code:
      class MyClass:
          class_variable = "Class-level variable"
      
      # Accessing class attribute
      print(MyClass.class_variable)
      
  4. How to define static variables in Python class:

    • Description: Define static variables directly within the class, outside of any method, and access them using the class name.
    • Example Code:
      class MyClass:
          static_variable = 42
      
      # Accessing static variable
      print(MyClass.static_variable)
      
  5. Python class attributes vs instance attributes:

    • Description: Class attributes are shared among all instances, while instance attributes are specific to each instance of a class.
    • Example Code:
      class MyClass:
          class_variable = "Class-level variable"
      
          def __init__(self, instance_variable):
              self.instance_variable = instance_variable
      
      # Accessing class and instance variables
      obj1 = MyClass("Instance 1")
      obj2 = MyClass("Instance 2")
      print(MyClass.class_variable)
      print(obj1.instance_variable)
      print(obj2.instance_variable)
      
  6. Static class variables example in Python:

    • Description: An example demonstrating the use of static class variables.
    • Example Code:
      class Car:
          wheels = 4
          mileage = 0
      
          def __init__(self, make, model):
              self.make = make
              self.model = model
      
      # Accessing static and instance variables
      car1 = Car("Toyota", "Camry")
      car2 = Car("Honda", "Civic")
      print(Car.wheels)  # Accessing static variable
      print(car1.mileage)  # Accessing instance variable
      
  7. Class-level variables and methods in Python:

    • Description: Class-level variables can be associated with class methods, allowing them to manipulate shared data.
    • Example Code:
      class MathOperations:
          result = 0
      
          @classmethod
          def add(cls, num):
              cls.result += num
      
          @classmethod
          def get_result(cls):
              return cls.result
      
      # Using class-level variables and methods
      MathOperations.add(5)
      MathOperations.add(3)
      print(MathOperations.get_result())  # Output: 8
      
  8. Python class variable scope:

    • Description: Class variables have class-level scope, meaning they are accessible within the class and among all instances of that class.
    • Example Code:
      class MyClass:
          class_variable = "Class-level variable"
      
          def print_class_variable(self):
              print(MyClass.class_variable)
      
      obj = MyClass()
      obj.print_class_variable()