Define a Python class, create two instances from it, and show that they hold separate state.
A class is a template; an object is an instance built from it. The class statement creates the template, and calling it creates an object.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof"
first = Dog("Rex")
second = Dog("Fido")
print(first.bark()) # Rex says woof
print(second.bark()) # Fido says woof
print(first is second) # False - two different objectsCalling the class runs its __init__ method, which stores the arguments on the instance. Each object has its own instance dictionary, so first.name and second.name are separate values and changing one does not affect the other.
The methods live on the class, not on the instances. Both objects share the same bark function, and the difference in what they return comes entirely from the self reference pointing at a different instance.
Two things about the mechanics are worth knowing. The class itself is an object — a type — and you can inspect it with Dog.__name__, Dog.__mro__ and type(first). And attribute lookup on an instance goes to the instance dictionary first and then to the class, which is the rule that makes class attributes behave differently from instance attributes.