Friday, May 20, 2016

An Introduction to Object Oriented Programming in Python - Part 2

How to Use OOP in Python

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

Right from the very first time you started using/learning python, you have been working with OOP concepts of “Objects” and “Methods” from classes without knowing properly about their existence.

For example;-
If you assign a string to a variable, what you did technically is that you created an object on that variable from the string class.
As seen below, the variable “name” is technically an object in python and we can call methods such as the upper() method on it.

name = "Umar Yusuf"
print type(name)  # confirms the type of object
print name.upper()  # using method on an object 

This is why it is said that: All most everything in python is an object.

More examples to show that almost everything in python is an object, are:-

number = 37
print (type(number))  # type int=""

a_float = 3.7
print (type(a_float))  # type float=""

a_list = ['abc', 'def', 12]
print (type(a_list))  # type list=""

def function():
print (type(function))  # type function=""

These two slide images below; obtain from this source summaries everything about "Real World" and "Software/Computer" Objects.

Examples of Real-World Objects that could be modeled into computer with OOP coding style

1) ATM
2) Bank Account
3) Fruit
4) Family
5) Cars
6) Shapes
7) Animal
8) Employee
9) User Registration Form
10) Hero
11) Football Club
12) Programmers Gallery
13) e.t.c

Note that virtually any object in the world can be modeled into computer/software code.

All of these objects listed above have one thing in common. They all have Attributes (properties) and Methods (behaviors).

These Attributes and Methods are usually defined within a "class" and a unique "instance or copy" of the "class" is technically called an "object".

So, it means for us to create or model any of the "objects" listed above in python OOP, we have to define a "class" and within the "class" we define "Attributes" and "Methods" of that "object" we want to model.

Now, this is better understood with an example; lets pick the "Car" object to sight an example

It is obvious that a Car object may have numerous "Attributes" and "Methods", but our code don't care about some of the car's details, so we have to limit the "Attributes" and "Methods" to what we need to keep track of in our project. This is a concept called "Data Abstraction" in OOP.

Assuming we settled to use this generic (blueprint) details below;-

~ Generic attributes/properties/variables of a car: brand, speed, height, weight, colour etc.
~ Generic methods/functions/behaviors of a car: activateHorn, moveForward, moveBack, turnRight, turnLeft etc.

Note that; for different objects, the values of attributes and methods can varies or be similar but behave differently.

Now that we have established some generic (blueprint), lets create the following cars:

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.

The code in python will look this... Lets continue in part 3

Reference Materials are here

No comments:

Post a Comment