Sobes.tech
Junior — Middle

Can you explain what static properties inside a class mean and how they are used?

sobes.tech AI

Answer from AI

Static properties (variables) inside a class in Python are variables that belong to the class itself, not to any specific instance. They are shared among all objects of that class.

They are used to store data that should be common to all instances, such as a counter of created objects or settings shared by all.

Example:

class MyClass:
    count = 0  # static property

    def __init__(self):
        MyClass.count += 1

obj1 = MyClass()
obj2 = MyClass()
print(MyClass.count)  # Will output 2

Here, count increases each time an object is created, showing the total number of class instances.