Classes and objects
Not met yet
A class is a pattern for making objects: class Bike: describes what every bike keeps and can do, and Bike(7, 4) makes one. Each object made from a class keeps its own attributes. Name a class for one thing, in CapitalisedWords: Bike, not bikes.
Try it
main.pyclass Bike:
def __init__(self, number, rate):
self.number = number
self.rate = rate
red = Bike(7, 4)
blue = Bike(12, 6)
print(red.number, blue.number)
Output
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks objects made from the same class share their attributes, so changing one changes them all.
- Thinks the class itself is one of the things it describes, so Parcel.weight works without making a parcel.