L4Β·OOP & Errors
ποΈClasses & Instances
Blueprints for objects with state and behavior.
classself__init__instance
Β§ 1Anatomy
class Name: body. __init__ initializes each new instance. self is the instance passed automatically.
Β§ 2Attributes
Instance attributes live on self. Class attributes are shared across instances.
Β§ 3Methods
Regular methods take self. @classmethod takes cls. @staticmethod takes neither.
Animated step-through
step 1 / 6// Dog('Fido', 3) β allocate + call __init__
1class Dog:β2 species = "Canis familiaris" # class attrβ3 def __init__(self, name, age):β4 self.name = nameβ5 self.age = ageβ6 def bark(self):β7 return f"{self.name} says woof"β8β9fido = Dog("Fido", 3)β10print(fido.bark())β11print(fido.species)βVariables
β none yet β
Call stack
<module>
Try it live Β· Python runs in your browser
Loading playgroundβ¦
Checkpoint quiz
Question 1 / 2
What does __init__ do?