Friday, May 20, 2016

An Introduction to Object Oriented Programming in Python - Part 3

Class, Attribute, Method, and Object

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

Let continue from where we stopped in Part 2.

We are about to model our car object in python code. So basically, we use the combinations of "Class", "Attribute", "Method", and "Object" to model a real world object into software code.

Lets recall the definitions for: Class, Attribute, Method, and Object

Class: A user-defined prototype/blueprint for an object that defines a set of attributes that characterize any object of the class.

The reason of creating a "class" is so we can create our own objects that have their attribute to do specific thing.
A "class" is created in python just by calling the class keyword follow by its name and a colon ( : ) at the end as seen below.

class ClassName:

Now within this class, we need to define the "Attributes" and "Methods" of our Car "Object". But before then, lets use an arbitrary example.

Attribute: A variable that is defined inside class (class attribute/variable) or inside a method (instance attribute/variable).

Method: A special kind of function that is defined in a class definition.

Lets add some attributes and methods to our arbitrary class above before we relate it to our "Car" object.

class ClassName:
 att1 = "Umar Yusuf"
 att2 = 24

 def mthd_one(self):
  att3 = "An instance attribute in Method_One"
  print att3

 def mthd_two(self):
  self.att4 = "An instance attribute in Method_Two"
  print self.att4

att1 and att2 are the class attributes
att3 and att3 are the method/instance attributes
mthd_one and mthd_two are the methods

If you observe the code, you will notice a strange word "Self" supplied as an argument in the methods. I will explain why that "self" in the next part 4.

The last but not the least is for us to utilize the class we created in the form of object. So lets create objects out of the class above, remember the definition of object?

Object: A unique instance/copy of a data structure that's defined by its class.

To create an object, we simply assign a variable to the class name as seen below. And to access the class attributes and methods, we simply use to dot notation by calling the object dot name of attribute or method that is: [object.attribute or object.method()] as seen below.

# To create an object "s" from the class
obj1 = ClassName()

# To access the class attributes
print obj1.att1
print obj1.att2

# To access the methods and their attributes

# Note: a method/instance attribute can NOT be accessed outside the method directly

Now, relating the knowledge above to our "Car" objects;-

Car 1
brand = Toyota
speed = 250km/hr
height = 1908mm
weight = 2800kg
colour = Green

Car 2
brand = Honda
speed = 180km/hr
height = 1500mm
weight = 3200kg
colour = Cyan

Car 3
brand = Ford
speed = 300km/hr
height = 2200mm
weight = 4200kg
colour = Red

Car 1, Car 2, and Car 3: activateHorn, moveForward, moveBack, turnRight, turnLeft are the methods to be used.

Our code should look like this for object Car 1:-

class Cars:
# Create and Set the class attributes to empty string or None keyword,
 # since we have multi car objects to instanciate
 brand = None
 brand = None
 speed = None
 height = None
 weight = None
 colour = None

# 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!")

# First object car1
car1 = Cars()

# Assign the attributes value of car1 object
car1.brand = "Toyota"
car1.speed = "250km/hr"
car1.height = "1908mm"
car1.weight = "2800kg"
car1.colour = "Green"

# Call any of the methods to see result

To create the second and third objects (Car 2 and Car 3) from the instance of the class Cars, all we have to do is assign object instance to the class name just the way we did above for Car 1 object. Here is the code:-

# Second object car2
car2 = Cars()

# Assign the attributes value of car2 object
car2.brand = "Honda"
car2.speed = "180km/hr"
car2.height = "1500mm"
car2.weight = "3200kg"
car2.colour = "Cyan"

# Call any of the methods to see result

# Third object car3
car3 = Cars()

# Assign the attributes value of car3 object
car3.brand = "Ford"
car3.speed = "300km/hr"
car3.height = "2200mm"
car3.weight = "4200kg"
car3.colour = "Red"

# Call any of the methods to see result

This a very basic and manual way of coding in Python OOP style. In the next Part 4 we will see how to improve the above code to be more dynamic and uses less codes.

See you in Part 4 ...Using Self keyword and Magic methods.

Reference Materials are here

No comments:

Post a Comment