SFWRTECH 3RQ3 - Introduction To Object Oriented Programming: Sean Watson
SFWRTECH 3RQ3 - Introduction To Object Oriented Programming: Sean Watson
Programming
Sean Watson
2 Getting Started
3 Object Orientation
Has Relationships
Is Relationships
4 Summary
Install
Find the latest stable version of Python(3.10.0 as of this writing)
from https://python.org.
Follow the installation instructions for your operating system.
Verify your install with the command python --version or
python3 --version
Note: Both Mac OS and most Linux distros include some version of
Python, make sure you have a modern version(3.6+) and not 2.x
Package Installer
Your Python install should have given you access to pip
This tool allows for installing Python packages from the command
line
We will want to use it to install PyTest with the following command:
python -m pip install pytest
Alternative: you may be able to install directly into your IDE
Python has decent ‘out of the box’ testing, but PyTest is an
excellent add-on to write clearer, more concise tests
IDE Install
In class demos will be in PyCharm, which can be downloaded from
https://jetbrains.com/pycharm/
If you prefer, VS Code is another good choice and can be downloaded
from https://code.visualstudio.com/(requires a Python
extension installed)
We will only be using plain, vanilla Python in exercises, do not worry
about any extra libraries that get mentioned
if __name__ == ’__main__’:
Now we know why the if statement gets true and why the next line
runs
But what about all that def stuff up at the top of our file?
1 def print_hi ( name ) :
2 print ( f ’Hi , { name } ’)
3
Let’s take a second look at our original file and figure out what it
does and why:
1 def print_hi ( name ) :
2 # Print this message to the console
3 print ( f ’Hi , { name } ’)
4
5 # Check if we are in the main
6 if __name__ == ’ __main__ ’:
7 print_hi ( ’ class of 3 RQ3 ’)
1 Defines a new method with one parameter we can call at any time
2 Checks to see if the __name__ variable is what we expect
3 If it is, it calls our print_hi method, which prints a string
4 If it is not, it calls nothing and just ends
Now we know the basics, let’s figure out how to make something
Object Oriented
To do this, we need to define what we mean
I We talked about objects in the last lecture, so we are familiar with this
I A programmed object implements the specification given by the
diagram
I We need all the attributes, behaviours and relationships of our class
diagram
Meet self
1 class GreetingPrinter :
2 """ This is a class for storing and printing
someone ’s name """
3 def __init__ ( self , name ) :
4 self . name = name
5
6 def print_name ( self ) :
7 print ( f ’Hi , { self . name } ’)
Has a relationships
Remember that in a has a relationship, we are discussing a
component of an object
I Menu has Menu Items
What does this remind us of?
I Attributes!
I An attribute doesn’t need to be a simple type like a number or string,
it can also be another object
1 class MenuItem :
2 """ This represents a selectable item from the
menu """
3 def __init__ ( self , name , category , price ) :
4 self . name = name
5 self . category = category
6 self . price = price
7
1 class Menu :
2 """ This represents a restaurant menu """
3 def __init__ ( self , items ) :
4 self . items = items
5
6 def g et _ a ll_by_category ( self , category ) :
7 return [ item for item in items if item .
category == category ]
8
So, our class Menu contains many items which are of type MenuItem
This follows from our earlier statement that Menu has MenuItems
So we can see then, that has a relationships can be modeled as
attributes
I The “owning” object, contains an attribute with the type of the
“contained” object
I These can be cases where we have exactly one, one-to-many or
zero-to-many relationships
Is a relationships
Remember that in this case one object is a special case of another
A daily special is definitely a menu item
I It needs to have the same information as a regular menu item
I However, it may have some extra attributes or may have different
methods
I So we say that the new class inherits the old class, and adds to it
1 class MenuItem :
2 """ This represents a selectable item from the
menu """
3 def __init__ ( self , name , category , price ) :
4 self . name = name
5 self . category = category
6 self . price = price
7
Now, consider that our Menu contains MenuItems, can some of these
be DailySpecials?
I Yes!
I We say that the signature of the subclass(DailySpecial) extends that
of the superclass(MenuItem)
I This means that we are guaranteed to find at least the properties of
the superclass
I We won’t look for more specific items, so it doesn’t matter what the
subclass adds
This relationship is a major part of OOP, and something we always
need to be on the lookout for
I It can potentially save us tons of extra coding and potential errors by
stopping us from writing the same code twice