Tutorial: Object Oriented Programming in Python – Part 6
Posted by Ed on December 10, 2011
This is the continuation of the Python OOP tutorial. Here, I will talk about modules in Python.
Before I address modules I want to describe how to avoid some tedious work in the Python-Shell. In the last few tutorials we always executed a file, e.g. vehicle.py, and used the Python-Shell to create objects and manipulate their attributes. Suppose the next day you wanted to review the commands you typed into the Python-Shell, then you would have to type every single command into the Python-Shell again. However, there is a way to avoid this. We can simply save our commands in a file and run the file.
from vehicle import Automobile from person2 import Person2 def myProgram(): a = Automobile(50, 120) a.setGear(3) a.status() print("\n") #print empty line a.accelerate(30) a.setGear(4) a.status() print("\n") #print empty line p = Person2("Alice", 25) p.talk() p.setFoodAndMusic("Ice Cream", "Rock Music")
Save the code in a file test.py and run it in the Python-Shell (e.g. in IDLE by pressing F5).
– Take a look at the function myProgram() in line 4. In the previous tutorials we took everything from line 05 to 15 and entered it in the Python-Shell. Now, everything we have to do is type myProgram() into the Python-Shell and all the commands in this function are executed:
>>> myProgram() You have just created a vehicle. The speed of the vehicle is 50 km/h. You have switched to gear 3. The speed of the vehicle is 80 km/h. You have switched to gear 4. You have just created a Person object. Hi! My name is Alice and I am 25 years old.
– Now, let’s talk about modules. In Python every file that ends with .py is a module, so examples for a module are vehicle.py, car.py and person2.py. In order to use a class from a module you simply use:
from moduleName import className
In our case we have a file test.py and would like to use the Automobile class. We do this by importing Automobile from the module vehicle.py, see Example line 01:
from vehicle import Automobile
– From the previous tutorial we know that the module vehicle.py (or simply vehicle) contains several classes, namely Vehicle, Automobile and Motorcycle. This is typical in Python. You take related or similar classes and merge them into the same module, i.e. file. Note that this is different to Java where every class has its own file. You may want to read the following websites on this issue:
How many Python classes should I put in one file? (stackoverflow.com)
Why NOT only one class per file? (bytes.com)
The unit in Java is a class, in Python the unit is a module. In Python a folder containing modules is a package.
– If you want to import every class from the module vehicle use the asterisk:
from vehicle import *
1a) Create a new file myFile.py. Import the class Car2 from car2.py and the Fruit class from fruit.py
1b) Write a function myProgram() that does the following:
– Create a Car2 object (an Audi with maximum speed 150 km/h).
– Print data about the created object.
– Create a Fruit object (a red and sweet apple)
Execute the function myProgram() in the Python-Shell.
1c) In the previous tutorials we decided the save the classes Car, Car2 and Person and Person2 in seperate files. Why was this not a wise idea with respect to Python? How should it be done?
1d) Are test.py and myFile.py modules?
1a) and 1b) File myFile.py
from car2 import Car2 from fruit import Fruit def myProgram(): # Create a Car2 object c = Car2("Audi", 150) c.printData() print("") # print empty line # Create a Fruit object f = Fruit("apple", "red", "sweet")
Executing the function myProgram() in the Python-Shell yields:
>>> myProgram() The car was manufactured by Audi , and its maximum speed is 150 km/h. The apple is red and tastes sweet.
1c) In Python we do not save each class in its own file. Instead we merge several related classes in a single file that we call module. It is wise to do the following:
Save the classes Car and Car2 in the file car.py (module).
Save the classes Person and Person2 in the file person.py (module)