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
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.
Static variables in Python classes:
class MyClass: static_variable = 42 # Accessing static variable print(MyClass.static_variable)
Class-level variables in Python:
class MyClass: class_variable = "Class-level variable" # Accessing class-level variable print(MyClass.class_variable)
Python class attributes and static variables:
class MyClass: class_variable = "Class-level variable" # Accessing class attribute print(MyClass.class_variable)
How to define static variables in Python class:
class MyClass: static_variable = 42 # Accessing static variable print(MyClass.static_variable)
Python class attributes vs instance attributes:
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)
Static class variables example in Python:
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
Class-level variables and methods in Python:
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
Python class variable scope:
class MyClass: class_variable = "Class-level variable" def print_class_variable(self): print(MyClass.class_variable) obj = MyClass() obj.print_class_variable()