Member-only story
Python OOP example: Car engine simulation for beginners
Strict Typing, DataClasses, Unit Tests, and everything else you need
In this article, I would like to show an example of a well-structured and tested Python project. This can be useful for those who start programming and for those who switch from another language to Python.
You will see why it is good to have strict typing in Python, how to test your code, how to use dataclasses, how to use override additions for your own classes, how to raise your own type of exceptions, and how to keep your code clean.
The main idea of the project is as follows (see the image above).
- We press a gas pedal, and it outputs some electrical current to the Gas Pump
- The Gas Pump is connected to a Gas Tank, inputs some portion of gas, and outputs it to the Engine
- The Engine should rotate
We want to be friendly to environmental-unfriendly diesel engines (though I really hate the weird sound of diesel engines), so we will be able to create any type of Gasoline, and any type of Engine (Benzin, Diesel, or whatever else). But if you supply some benzine to a diesel engine (or vice versa), it should raise an exception. Though it should be allowed to use different octane numbers of the same type of gasoline, and it should handle…