Paul's Python CoursePPC

__init__

Not met yet

__init__ runs by itself every time an object is made. Its job is to give the new object its starting attributes, from the arguments and from sensible starting values like 0 or None. It returns nothing: Bike(7, 4) hands back the new object on its own.

Try it

main.py
class Ticket:
    def __init__(self, route, price):
        self.route = route
        self.price = price
        self.used = False

t = Ticket("Leeds", 3)
print(t.route, t.price, t.used)

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks attributes set in __init__ cannot be changed afterwards.
  • Thinks __init__ must be called by hand after making an object.

Where it comes up