Sunday, May 22, 2016

An Introduction to Object Oriented Programming in Python - Part 5

Using "Modules" and "Import" Statement


Make sure you read Part 1, Part 2, Part 3 and Part 4 before you continue reading...

In this part, we will copy the class creation code into a module (file) named "car_class.py" and the class usage (object) code copied into another separate file named "car_object.py".

You can use whatever names for the files as long as you remember which file contains which code.

So, the two files should look like this:-

car_class.py


class Cars:
# Create and Set the class attributes using constructor (__init__) method
 def __init__(self, brand, speed, height, weight, colour):
  self.brand = brand
  self.speed = speed
  self.height = height
  self.weight = weight
  self.colour = colour
  
# Create the methods with simple "print" statement in them
 def activateHorn(self):
  print ( self.brand + " Says: Poooooooorh!")

 def moveForward(self):
  print ( self.brand + " Moving Forward at the speed of: " + self.speed)

 def moveBack(self):
  print ( self.colour + " " + self.brand + "  is Moving Backward!")

 def turnRight(self):
  print ("Turning Right!")

 def turnLeft(self):
  print ("Turning  Left!")


car_object.py


# Creates First object car1 and Assign the attributes value of car1 object
car1 = Cars("Toyota", "250km/hr", "1908mm", "2800kg", "Green")

# Creates Second object car2 and Assign the attributes value of car2 object
car2 = Cars("Honda", "180km/hr", "1500mm", "3200kg", "Cyan")

# Creates Third object car3 and Assign the attributes value of car3 object
car3 = Cars("Ford", "300km/hr", "2200mm", "4200kg", "Red")

# Call any of the methods on any of the objects to see result
car1.activateHorn()
car2.activateHorn()
car3.activateHorn()


By separating the file, we created our Cars class in "car_class.py" and "car_object.py". So, to run the code we make use of "car_object.py".

Now if you run: car_object.py, you will see a "NameError: name 'Cars' is not defined".

That is true of the error because we no longer have 'Cars' class in that module/file.

To solve this, we make use of the "import" statement. The "import" statement can take different forms as listed below;-
~ import modname
~ from modname import name1
~ from modname import *

Where "modename" is the name of the module/file we are importing in our case it is: car_class.py

So we can now edit our car_object.py module/file to look like this (if using: import modname):-

import car_class


# Creates First object car1 and Assign the attributes value of car1 object
car1 = car_class.Cars("Toyota", "250km/hr", "1908mm", "2800kg", "Green")

# Creates Second object car2 and Assign the attributes value of car2 object
car2 = car_class.Cars("Honda", "180km/hr", "1500mm", "3200kg", "Cyan")

# Creates Third object car3 and Assign the attributes value of car3 object
car3 = car_class.Cars("Ford", "300km/hr", "2200mm", "4200kg", "Red")

# Call any of the methods on any of the objects to see result
car1.activateHorn()
car2.activateHorn()
car3.activateHorn()


Or like so (if using: from modname import ...):-

from car_class import Cars
# from car_class import *

# Creates First object car1 and Assign the attributes value of car1 object
car1 = Cars("Toyota", "250km/hr", "1908mm", "2800kg", "Green")

# Creates Second object car2 and Assign the attributes value of car2 object
car2 = Cars("Honda", "180km/hr", "1500mm", "3200kg", "Cyan")

# Creates Third object car3 and Assign the attributes value of car3 object
car3 = Cars("Ford", "300km/hr", "2200mm", "4200kg", "Red")

# Call any of the methods on any of the objects to see result
car1.activateHorn()
car2.activateHorn()
car3.activateHorn()


That is it.
In the next Part 6, we will discuss the three pillars of OOP: Encapsulation, Inheritance and Polymorphism. See you there.


No comments:

Post a Comment