Classes and instance state
Learn in depth →A class defines behavior; instances hold their own state through attributes.
class Name:
def __init__(self, value): ...Example
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
c = Counter()
c.increment()
print(c.value)Result
1
Watch for: A mutable class attribute is shared across instances unless shadowed. Initialize per-instance lists in __init__.
Practise this topic →