0% found this document useful (0 votes)
2 views12 pages

Python Object-Oriented Programming Guide

Uploaded by

yy7128273
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Python Object-Oriented Programming Guide

Uploaded by

yy7128273
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER PROGRAMMING

(MEE 324)

by

Engr Dr Okoro Uzoma Gregory


(BEng — MEng — PhD)
Lecturer in Design and Production
Engineering

An Undergraduate course in Mechanical Engineering


Federal University of Technology Minna
Contents

1 Introduction 2
1.1 Python: Procedural or Object-Oriented Programming? . . . . . . 2

2 The Python Syntax 4


2.1 Going Deep into the ’self’ Variable . . . . . . . . . . . . . . . . . 5
2.1.1 Some Notes About Object-Oriented Programming . . . . 6
2.2 Changing the values in objects: method vs direct . . . . . . . . . 7
2.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 More Examples of class and objects 10


3.1 Creating and using your own type with classes . . . . . . . . . . . 10

1
Chapter 1

Introduction

1.1 Python: Procedural or Object-Oriented Program-


ming?
When you first learn a program, you are seemingly using a technique called pro-
cedural programming. A procedural program is typically a list of instructions that
execute one after the other starting from the top of the line. On the other hand,
object oriented programs are built around well objects. You can think about ob-
jects as something that exists in the real world. For instance, if you were going to
establish a shoe store, the store itself would be an object. The items in the store,
for example, boots and sandals, would also be objects. The cash register would be
an object, and even a salesperson would be an object.

Object-oriented programming has several advantages over procedural program-


ming, which is the programming style you most likely first studied;
1. Object-oriented programming enables you to develop large, modular pro-
grams that can instantly expand over time.
2. Object-oriented programs hide the implementation from the end-user. Ob-
jects in an OOP language provide an abstraction that hides the internal im-
plementation details. The programmer just need to know which methods
of the object are available to call and which input parameters are needed
to trigger a specific operation. But you don’t need to understand how this
method is implemented and which kinds of actions it has to perform to cre-
ate the expected result.
Object-oriented program would focus on the specific characteristics of each
object and what each object can do. An object has two fundamental parts, charac-
teristics, and actions. For example, several characteristics of a salesperson would

2
include the person’s name, address, phone number, and hourly payout, also what
a salesperson can do. That might involve selling an item or taking items from a
warehouse. As another example, shoes’ characteristics could be the colour, size,
style, and price. What actions could a shoe take? A shoe is an inanimate object,
but it could, for example, change its price.
An object has characteristics and actions. Characteristics have a specific name.
They are called attributes, and the actions are called methods. The colour, size,
style, and the price would be called attributes in the shoe example. The action
of changing the price would be a method. There are two other terms, which are
object and class. An object would be a specific shoe, for instance, a brown shoe
with a US size 7.5, and sneaker style with a price of $110 in the shoe example.
This brown shoe could change its price. Another object would be something like,
a white color shoe with a US size 4.5, and a flip-flop shoe that costs $80. This
white shoe could change its price as well. You will notice that the brown shoe
and white shoe both have the same attributes. In other words, both shoes have a
color, size, style, and price. Notice also that they have the same method. It is like
they came from a blueprint–i.e., a generic shoe consisting of all the attributes and
methods. This generic version of an object is called a class.

You only need to classify this blueprint known as class one time, and then you
can create specific objects from the class over and over again. In other words, you
can use the shoe blueprint, to make as many shoe objects as you want, in any size,
shape, color, style, and price. These are fundamental terms in object-oriented pro-
gramming.

In summary, the Object-Oriented Programm1ing (OOP) Vocabulary are;

Class : a blueprint which is consisting of methods and attributes.

Object : an instance of a ctass. It can hetp to think of objects as something in


the real world like a yellow pencil, a small dog, a yellow shoe, etc. However,
objects can be more abstract.

Attribute : a descriptor or characteristic. Examples would be colour, length,


size, etc. These attributes can take on specific values like blue, 3 inches, large,
etc.

Method an action that a class or object could take. − − − − − − − − − − − −


−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
− − − − − − − − − − − − − − − − − − − − − − − − − − − − −−

3
Chapter 2

The Python Syntax

The python synthax can be demonstrated using the image shown in ?? Figure2.1,
contains the synthax for creating a Shoe class. The class has color, size, style,
and price attributes. It also has a method to change the price, as well as a method
to output a discounted price. Inside the Shoe class, some codes come to shows
examples of how to instantiate shoe objects so that you can see how to use a class
in a program. Remember that a class represents a blueprint. Hence we are setting
up the color, size, style, and price of a generic shoe. Two things about the synthax
in 2.1 might seem a bit odd:

1. init −→ Python built-in function

2. self −→ variable

Python use init to create a specific shoe object. On the other hand, the self
variable can be a bit tricky to understand in the beginning. self saves attributes
like color, size, and so on, making those attributes available throughout in the
Shoe class. self is essentially a dictionary that holds all of your attributes and the
attribute values -checkout the change price method to see how the self works. We
can pass in self as the first method input to have access to the price. The self
has the price stored inside it, as well as the other attributes like color, size, and
style. self will always be the first input to your methods if you want to access the
attributes. Note as well that the change price and the discount methods are like
general Python functions. For instance, Python functions do not have to return
anything, so like in the change price method, it does not return anything. It only
changes the value of the price attribute. The discount method, however, does
return something it returns the discounted price.

Additional: Function vs Method A function and method seem very similar;


both of them use the same keyword. They also have inputs and return outputs.

4
Figure 2.1: Synthax for creating Shoe Class

Figure 2.2: Using ’self.’

The contrast is that a method is inside a class, whereas a function is outside of a


class.

2.1 Going Deep into the ’self’ Variable


If you define two objects, how does Python differentiate between two objects?
That is where self comes into play. If you call the change price method on
shoe one , how does Python know to change the price of shoe one and not of
shoe two?
self tells Python where to look in the computer’s memory for the shoe one
object, and then Python changes the price of the shoe one object. When you call
the change price method, shoe [Link] price(125), self is implicitly passed in.

5
Figure 2.3: Implementing a class

The word self is just a convention. You could actually use any other name as long
as you are consistent; however, you should always use self rather than some other
word, or else you might confuse people.

2.1.1 Some Notes About Object-Oriented Programming


Now, we are going to write a separate Python script that uses the shoe class code.
In the same folder, you need to create another file called [Link]. Inside this
file, we want to use the shoe class. First, you need to import the shoe class by
typing from shoe import Shoe. The lowercase shoe refers to the [Link] file,
while the uppercase shoe refers to the class defined inside this file.
You could specify the file and class anything you wanted to. They do not
have to be the same. We just did this for convenience. Now, in the [Link],
you can use the shoe class. If you noticed, the code is now modularized. You
will write some code that uses the shoe class. The shoe class has a method to
change the price of the shoe. In [Link] file, it can be seen (in line 9) that
shoe [Link] price(98). In Python, you can also change the values of an at-
tribute with the following syntax, shoe [Link] = ’blue’ , shoe [Link] = 5.0,
and shoe [Link] = 78. There are some drawbacks to accessing attributes di-

6
rectly versus writing a method for accessing and displaying attributes.

2.2 Changing the values in objects: method vs di-


rect
Changing values through a method gives you more flexibility in the long-term. But
if the units of measurement replaced, for example, the store was initially meant to
serve in US dollars and now has to work in euros. If you have changed an attribute
directly like in 2.2 shoe [Link] = 78, if all of a sudden you have to use euros,
you are going to have to modify this manually. You are going to have to do this
wherever you access the price attribute directly. If, on the other hand, you would
use a method like the change price method, then all you have to do is go into the
shoe class and change the original method one time. We are going to multiply by,
for example, 0.81 to convert everything from dollars to euros. If you go back to
[Link], a line like a number 9, you do not have to manually change the price
from dollars to euros because the conversion will take care of that for you in the
shoe class.

2.3 Inheritance
In the context of our study, inheritance will be thought f using a real-world object
like the shoe example explained earlier. The shoe had four attributes, color, size,
style, and price. The shoe also had two methods: a method to change the price, and
a method for calculating a discounted price. As the store expands, it might stock
other types of shoes like heels, ankle boot, and mules. These footwears have a few
attributes and methods in common with the shoe object. They probably all have
a color, size, style, and price and, they could all use functions to change the price
and calculate a discount. Why code separate classes for each new footwears when
they all have so much in common? Alternatively, you could code apparent shoe
class, and then the heels, ankle boot, and mules class could inherit the attributes
and methods of the shoe class as shown in figure 2.4.
This looks like a family tree where the shoe is the parent, and heels, ankle
boot, and mules, are the children. One benefit is that, as you add more shoe types
like ballerinas, you can easily add a new class inheriting from the shoe class. What
if you wanted to add a new attribute like material to represent if the footwear is
made of synthetic, rubber, or foam? Now, all you have to do is, add a material
attribute to the shoe class, and all the children’s classes automatically inherit the
new attribute. Your code becomes more efficient to write and maintain.

7
Figure 2.4: Demonstrating inheritance in python

8
2.4 Conclusion
Python is considered as an object-oriented programming language rather than a
procedural programming language. Thus far, the course has exposed the students
to the core of object-oriented programming languages in Python such as:

1. classes and objects

2. attributes and methods

3. inheritance

9
Chapter 3

More Examples of class and objects

3.1 Creating and using your own type with classes


Just like in function, we are going to separate the code we talked about today and
a code where you implement a data type and a code where you use an object that
you create. Remember when we talked about functions you where thinking about
it in terms of writing a functions so you had to worry about the details of how
you implement a function and you had to worry about how to use the function.
The same idea is applicable in OOP. If you are thinking of implementing your
own data type, you do that using a class. When you create a class, you give
it a name and attributes—the attributes are going to be the data representation
and the ways you are going to interact with the object. You, as a programmer,
are going to decide how you want people to interact with the object and what
data representation the object will have. For example someone wrote a code that
implements a list of class. We don’t actually know how that was done but we
can actually find out. Creating a class means figuring out data representation and
means of implementing the class. Once that is done, you can then use your class.
This means creating instances of the class. That is creating new object that has the
type and the name of your class, you can create any number of objects that you
like do any kind of operation that you have define on the class. Now lets write
classes which gonna define your own kind of object.

10
Bibliography

11

You might also like