Welcome to the Architect's core skill: Object-Oriented Programming (OOP).
Until now, we used built-in objects (strings, lists). Now, we will create our own types of objects.
Blueprints (Classes) vs Houses (Objects)
Think of a Class as a blueprint. It's just a drawing on paper. Think of an Object as the actual house built from that blueprint.
You can build infinite houses from one blueprint!
# The Blueprint (Class) class Cat: pass # The Actual Cats (Objects) kitty = Cat() luna = Cat()
Define a class named Robot. Don't forget the colon :! Inside, just write pass for now (it means "do nothing").
Create an instance of your robot: bot1 = Robot().
Create another instance: bot2 = Robot(). They are two separate robots built from the same plan!
Notice how we use CapitalLetters for Class names (like Robot), but lowercase for variables (like bot1). This is a Python tradition.
Suggested SolutionExpandCollapse
# 1. The Blueprint class Robot: pass # 2. Build the Bots bot1 = Robot() bot2 = Robot() # 3. Verify print(bot1) print(bot2)
Advanced TipsWant more? Click to expandClick to collapse
F. Everything is an Object
- In Python, everything is an object.
"hello"is an object of classstr.42is an object of classint.[1, 2, 3]is an object of classlist.- Now you can make your own!