1.Classes and objects
A class defines the shape something has — its attributes and behaviour. An object is one concrete thing built from that shape. class Dog: defines the blueprint; Dog() builds an object from it.
Key features
- class Name: defines a blueprint — it runs no instance-specific code by itself
- Name() calls the class, which builds and returns one new object
- Each object built this way has its own independent set of attributes
- type(obj) reports the class that built an object; isinstance(obj, Name) checks it
- A class is itself an object too — of type type — which is why Name() can be called
Class vs. the objects it builds
- class Dog — the blueprint — no dog yet
- Dog() — called twice, independently
- rex, fido — two distinct objects, same shape
Command
class ClassName:
"""A blueprint — this body defines shape, not one instance."""
...
obj = ClassName() # calling the class builds one objectA class is the blueprint; Name() builds one independent object from it. type(obj) and isinstance(obj, Name) both ask which blueprint built it.