Exemple #1
0
# Importing classese
# As we add more functionality to our classes, our files can very long, even when using inheritance properly. As such we should move them into their own modules.

# Importing a single class
from cars import Car

# After importing the module cars, we can use the class Car from it as if the class was in this file
myNewCar = Car("audi", "a4", 2019)
print(myNewCar.getDescriptiveName())

myNewCar.odometerReading = 23
myNewCar.readOdometer()
Exemple #2
0
# Importing multiple classes from a module
# We can import as many classes as we need into a program file

# We import multiple classes from a module by separating each class with a comma
# Once imported, we are free to use each class as needed
from cars import Car, ElectricCar

myBeetle = Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing an entire module
# We can also import an entire module and then access the classes we need by using dot notation. This approach is simple and results in code that is easy to read.
print("\n")
# Here we import the entire cars module and we access it by using moduleName.ClassName syntax
import cars

myBeetle = cars.Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = cars.ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing all classes from a module
# from moduleName import *

# This method is not recommended for a few reasons.
# One with this approach it's unclear which classes we're using from the module. This results in confusion with names in the file. It can result in importing another module that has the same naming for classes which will conflict and or overwrite your classes. This can lead to difficult dianoses.