🧭Constructor
Also known as __init__, Constructor Method, Initializer
A method that runs when a new object is made. It sets the first values.
Think of it like
Think of it like a name tag. You fill it in the day a new pet comes home.
Example
When Emma writes Dog("Rex"), the constructor runs and saves Rex as the dog's name.
Why it matters
Definition
Example code
python
class Dog:
def __init__(self, name):
self.name = nameCommon mistake
Key takeaways
- 1Runs once. When the object is made.
- 2Sets starting values. From what you pass in.
- 3Different names. __init__ or constructor.