How are classes better?

Imagine you have a program that calculates the velocity of a car in a two-dimensional plane using functions. If you want to make a new program that calculates the velocity of an airplane in three dimensions, you can use the concepts of your car functions to make the airplane model work, but you'll have to rewrite many of the functions to make them work for the vertical dimension, especially to map the object in a 3D space. You may be lucky and be able to copy and paste some of them, but for the most part, you'll have to redo much of the work.

Classes let you define an object once, and then reuse it multiple times. You can give it a base function (functions in classes are called methods to indicate that they are object-oriented) ...

Get Learn Programming in Python with Cody Jackson now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.