Ob Ject Oriented Python
Ob Ject Oriented Python
Ob Ject Oriented Python
3
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
4
Classes
A Python class uses variables to store data fields and defines methods to perform
actions. Additionally, a class provides a special type method, known as initializer,
which is invoked to create a new object. An initializer can perform any action, but
initializer is designed to perform initializing actions, such as creating the data fields
of objects.
class ClassName:
initializer
methods
5
Constructing Objects
Once a class is defined, you can create objects from the class by using the
following syntax, called a constructor:
className(arguments)
6
Constructing Objects
7
Instance Methods
Methods are functions defined inside a class. They are invoked by objects to
perform actions on the objects. For this reason, the methods are also called
instance methods in Python. You probably noticed that all the methods including
the constructor have the first parameter self, which refers to the object that invokes
the method. You can use any name for this parameter. But by convention, self is
used.
8
Accessing Objects
After an object is created, you can access its data fields and invoke its methods
using the dot operator (.), also known as the object member access operator. For
example, the following code accesses the radius data field and invokes the
getPerimeter and getArea methods.
9
Why self?
Note that the first parameter is special. It is used in the implementation of the
method, but not used when the method is called. So, what is this parameter self
for? Why does Python need it?
self is a parameter that represents an object. Using self, you can access instance
variables in an object. Instance variables are for storing data fields. Each object is
an instance of a class. Instance variables are tied to specific objects. Each object
has its own instance variables. You can use the syntax self.x to access the instance
variable x for the object self in a method.
10
Example: Defining Classes
and Creating Objects
TV
channel: int The current channel (1 to 120) of this TV.
volumeLevel: int The current volume level (1 to 7) of this TV.
on: bool Indicates whether this TV is on/off.
11
Trace Code Assign object
reference to myCircle
myCircle = Circle(5.0)
myCircle reference value
yourCircle = Circle()
radius: 5.0
12
Trace Code
myCircle = Circle(5.0)
myCircle reference value
yourCircle = Circle()
radius: 5.0
Assign object
reference to
yourCircle
: Circle
radius: 1.0
13
Trace Code
myCircle = Circle(5.0)
myCircle reference value
yourCircle = Circle()
radius: 5.0
Modify radius in
yourCircle
: Circle
radius: 100
14
The datetime Class
from datetime import datetime
d = datetime.now()
print("Current year is " + str(d.year))
print("Current month is " + str(d.month))
print("Current day of month is " + str(d.day))
print("Current hour is " + str(d.hour))
print("Current minute is " + str(d.minute))
print("Current second is " + str(d.second))
15
Data Field Encapsulation
To protect data.
To make class easy to maintain.
To prevent direct modifications of data fields,
don’t let the client directly access data fields.
This is known as data field encapsulation. This
can be done by defining private data fields. In
Python, the private data fields are defined with
two leading underscores. You can also define
a private method named with two leading
underscores.
CircleWithPrivateDataRadius
16
Data Field Encapsulation
CircleWithPrivateDataRadius
18
Class Abstraction and Encapsulation
Class abstraction means to separate class implementation
from the use of the class. The creator of the class provides a
description of the class and let the user know how the class
can be used. The user of the class does not need to know
how the class is implemented. The detail of implementation
is encapsulated and hidden from the user.
19
Designing the Loan Class
The – sign denotes a private data field.
The get methods for these data fields are
provided in the class, but omitted in the
UML diagram for brevity.
Loan
-annualInterestRate: float The annual interest rate of the loan (default: 2.5).
-numberOfYears: int The number of years for the loan (default: 1)
-loanAmount: float The loan amount (default: 1000).
-borrower: str The borrower of this loan.
20
Object-Oriented Thinking
This book’s approach is to teach problem solving
and fundamental programming techniques before
object-oriented programming. This section will
show how procedural and object-oriented
programming differ. You will see the benefits of
object-oriented programming and learn to use it
effectively. We will use several examples in the rest
of the chapter to illustrate the advantages of the
object-oriented approach. The examples involve
designing new classes and using them in
applications.
21
The BMI Class
The get methods for these data fields are
provided in the class, but omitted in the
UML diagram for brevity.
BMI
-name: str The name of the person.
-age: int The age of the person.
-weight: float The weight of the person in pounds.
-height: float The height of the person in inches.
BMI(name: str, age: int, weight: float, Creates a BMI object with the specified
height: float) name, weight, height, and a default age
20.
getBMI(): float Returns the BMI
getStatus(): str Returns the BMI status (e.g., normal,
overweight, etc.)
22
Procedural vs. Object-Oriented
In procedural programming, data and
operations on the data are separate,
and this methodology requires
sending data to methods. Object-
oriented programming places data
and the operations that pertain to
them in an object. This approach
solves many of the problems
inherent in procedural programming.
23
Procedural vs. Object-Oriented
The object-oriented programming approach
organizes programs in a way that mirrors the
real world, in which all objects are associated
with both attributes and activities. Using
objects improves software reusability and
makes programs easier to develop and easier
to maintain. Programming in Python involves
thinking in terms of objects; a Python program
can be viewed as a collection of cooperating
objects.
24
Course outcomes
• Be able to choose diverse professional careers in
software industry, research, academia, engineering and
administrative services.
• Be able to apply the principles of Computer sciences
and Information Technology to solve real world
problems using digital computing systems and
programming languages.
• Be able to analyze, design, code and evaluate robust,
scalable and cost-effective software in the industry.
• Be aware of professional and ethical practices in the
context of social impacts of Programming language.
25
References
• Introduction to Programming Using Python by Y. Daniel Liang