Until now, we've used Python's built-in types (strings, lists, dictionaries). This chapter introduces the ability to create our own programmer-defined types using classes.
- Class: A blueprint or template for a user-defined type.
- Instance: A specific object created from a class.
- Instantiation: The process of creating a new object.
- Attributes: Named values associated with an object (e.g.,
p.xandp.y). - State: The values of an object's attributes at a given time.
- Classes vs Instances: A class is like the concept of a "Car". An instance is your specific Honda Civic parked outside.
- Objects are Mutable: By default, you can change the state of an object by reassigning its attributes. This is powerful but requires careful tracking of aliasing.
- Copying: Because objects are mutable, aliasing (
p1 = p2) can lead to bugs. Use thecopymodule (copy.copy()orcopy.deepcopy()) to create independent clones of objects.