["python","oop"]
What difference does it make to use "self" to define a member in a Python class?

How do these 2 classes differ? class A(): x = 3 class B(): def __init__(self): self.x = 3 Is there any significant difference?
A.x is a class variable. B's self.x is an instance variable. i.e. A's x is shared between instances. It would be easier to demonstrate the difference with something that can be modified like a list: class A: x = [] def add(self): self.x.append(1) class B: def __init__(self): self.x = [] def add(self): self.x.append(1) x = A() y = A() x.add() y.add() print("A x:", x.x) x = B() y = B() x.add() y.add() print("B x:", x.x) Output A x: [1, 1] B x: [1]