0% found this document useful (0 votes)
10 views

LectureThree - OOP in Action

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

LectureThree - OOP in Action

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PROGRAMMING Lecturer: K. N.

MATINGO
FOR GEOMATICS
[email protected]

APPLICATIONS
HSVG245

To be continued

OOP
IN ACTION
(APPLICATIONS OF OOP) 74

LECTURE OUTLINE
• Introducing OOP
• Difference with procedural programming

• Rationale for using OOP and when to use it

• OOP concepts

2
Image Source: TechTarget

OBJECT ORIENTED VS
PROCEDURAL
methods are used to operate on the object’s
OOP own data structure
• Centres on the use of objects that group Recall
properties and methods together • These

• The code is often broken up and distributed across multiple files, each with different
purposes

Procedural
• Separation between data and procedures
• Code executed from top to bottom
• The variables are passed to/through procedures and operated on, rather than
operating on themselves
3
OOP – WHY BOTHER?
• Useful when you want to group data and
behaviour together in pockets of
functionality

• Easier to handle when programs represent more


complex objects (e.g. representing lines is
easier
using points than using several x coordinates and
y coordinates)

• Good when writing several similar models –


enable
reuse of some objects 4
Aris Paschalidis

OBJECTS AND CLASSES


• A class is a representational model (a blueprint) of some aspect of
reality (things), of what makes up a typical ‘thing’ (its state
variables) and what it can do (its behaviour).

• An object is a single (particular) instance of a class.

• Real world objects share two characteristics:


• State: the particular properties/condition that the object has/is in at
a specific time
• Behaviours: the potential ways in which the object can
function

• Take a radio as an example real world object, for instance:


• What is the state of this radio?
5
• What are the possible ways the radio can function (i.e. its
behaviours)?
OBJECTS AND CLASSES (CONT’D)
• A class is a user-defined type

• An object is a single instance of the class

• Thus an object receives (inherits) all the characteristics


(potential properties and behaviours) of the class

6
MAKING A SIMPLE CLASS

• As geomaticians we are interested in measuring or setting-out locations, i.e.


points

• We could create a simple class for this, say a 2D point

2D point

• What would you ask for to help you describe (define) a basic 2D point?

7
SIMPLE 2D POINT CLASS

State
Y coordinate

X coordinate Colour

8
How would you represent these states in a computer program using the Python data
types that we have already met?
SIMPLE 2D POINT CLASS (CONT’D)

State
Y coordinate
Floating point?

Colour
X coordinate
Integer?
Floating point? String?
Integer?

How to
choose which
properties to
represent the
Object by?
• Need to represent the point sufficiently to fulfil the application/program’s purpose 9
How to choose which data types to represent the property?
• Depends what operations are required to manipulate the object
ARCHITECTURE OF AN OBJECT
ORIENTED PYTHON
APPLICATION
• Modules (.py files): store logical groups of functions, constants and classes together in
the same file.

Project
Module A Functions
useful together
way modules that are
Module B Functions for

Module C Functions

• Project: a grouping
Class (Bi) Class (Ci) Cla
Any number of constants, Class (Cii)
created for use ss (Aii)
Cla specific application
ss (Ai) within a functions, classes within a Class (Bii)
module
Class (Cn) 10
Cla
ss (An) Class (Bn)

CREATING MODULES WITHIN A PROJECT

• Usually we find that modules are saved to the same location as the project • However
we may find the need to reuse (import) modules created in different projects • Typical
Python style: put related classes in the same module
• It may be that the class ends up in a module of its own (especially if the class is too
large) but this should not be the goal
• It is less confusing if the class and the module do not have the same names (different
from Java!)

• For our Point 2D example, we create a folder for our project, then a module Point.py

• Within the module we can store the Point2D class and a Point3D class if we choose to make
one11
DRIVER?

• Often applications that work with


multiple classes of objects (and
therefore multiple modules/classes)
use
a ‘driver’ module that controls
(drives)
the program

• This is written ina separate .py file


that accesses (by importing) the
individual modules and classes
saved within (and outside of) the
project
12
CLASS COMPONENTS

• All Object Classes are set up as follows:


class Classname (base-classes):
statement(s)
• Classname:
• The name used to create new objects. Traditionally, classname is Capitalised.
• Base-class:
• The name of the base class, from which this class can inherit attributes and operations. For
simple classes, we define the base-class as object
• Methods (functions) to:
• Define the state of the Object instance
• Dictate the behaviour of the Object
13
CLASS COMPONENTS: CONSTRUCTOR
METHOD
• All classes have a constructor method, defined by the method name init

• When you create an instance of an object, python implicitly ‘calls’ the class’
constructor method to set up the state of the instance

• It passes the object as the parameter ‘self’ to the init method as well as any
other parameters required to instantiate the object

• If we don’t define an init method, no instance-specific state will be set


class classname (Object):
def init (self, parameters):
statement(s)
14
PRACTICAL CODING SESSION
Click here to Access GitHub Repository with Code Examples

Click here to

→Access GitHub Repository with


Code Examples

14
CONSTRUCTOR FOR OUR SIMPLE
POINT CLASS
Class statement
must always specify
‘self’ as parameter

This dictates
that the
information to
create a
Ponint2D object, an X and Y

We bind the x and y coordinate values (passed


as parameters to the constructor method) to
the instance of the Point2D object. Once
bound, these are known as instance variables
coordinate must be provided
15
CREATING OUR FIRST
POINT2D INSTANCE
• In the driver class …
1. Import the Point2D class from the Point
module, similar to how we imported the
pyplot class from the matplotlib module so
that we could use the scatter method

2. Invoke the class object as if it were a


method/function

3. Press
You get an error if the correct
class is not imported
16
If the correct number of parameters are not
provided
ACCESSING INSTANCE VARIABLES

• There will be many occasions when we will want to access an Object’s instance
variables (i.e. those variables bound to an object within the constructor method)
from either our Driver module or other module/class
• The method considered ‘best practice’ for Python programming is to use the dot(.)
operator:
object.variable_name

• Try it yourself

17
BEHAVIOUR (METHODS)

Make a relative
movement
(translation)
Clone
Re-project

Software object behaviour is more abstract than that of a real-life object •


They tend to have a wider range of behaviour that real-life objects
• Methods can be created to:

• Operate on the state of the object


18
• Find out information about the object or its variables

• Methods also serve as primary mechanisms for object to object communication


CLASS METHODS
• Methods are functions (behaviors) that belong to objects of the class
• They are defined and used in the same way as other functions
of the method
The name of the method. This is the
Like in the constructor method,
name used to invoke the method, so it
self represents the object instance
is useful that it represents the purpose

def methodname(self, parameters):


statement(s)

Any number of statements that are


executed once the method has been the object. Most often a return Parameters required by the method to
invoked. These manipulate the state of statement successfully accomplish its purpose
the object i.e. access information about
19
OUR FIRST POINT2D METHOD
2
• Our first method to move
(translate) the Point2D
object

1
3
20
THE RETURN
STATEMENT
• The idea is that if we invoke a method (i.e. a function) at some point in our code,
the method ‘returns’/inserts a value to the point in the code from which we
invoked the method
• In effect, the call to the method is directly substituted by the value that the
method returns
• The return statement is allowed only inside a method/function body
• When return executes the function terminates, and the value of the expression is
the function’s result.

• Returns None if:


• Reaches the end of the body
• Executing a return statement that has no expression
• Executing return None
21
ASSIGNMENT THREE 3. Move (translate) the points
4. Replot them in a different colour
1. Create a Python class to represent a simple point class as

covered in this lecture 2. Write a driver program to: • What stages are involved??

1. Import X and Y coordinates from a text file


2. Plot the coordinates on a scatterplot
Submit code via GitHub repository

22

You might also like