Changes

Jump to: navigation, search

OPS435 Python Lab 7

3,652 bytes added, 09:28, 21 January 2020
no edit summary
<font color='red'>
'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''
</font>
= LAB OBJECTIVES =
:Object-oriented programming is one level of complexity higher than simply structured programming as you've experienced in languages such as Bash or C. In this second lab on objects we're going to look at slightly more complex issues that come up when using them.
== PYTHON REFERENCE ==
:In previous labs, you have been advised to make notes and use online references. This also relates to learning about objected object oriented programming to help becoming "overwhelmed" with the volume of information in this lab.
:Below is a table with links to useful online Python reference sites (by category). You may find these references useful when performing assignments and labs.
{| class="wikitable" | style="margin-left:20px; border: 2px solid black;"
|- style="border: 2px solid black;font-weight:bold;text-align:center;"
| style="border: 2px solid black;" | Category
| style="border: 2px solid black;" | Resource Link
|- style="background-color:white;border:none;"
| style="border: 2px solid black;" valign="top"| :Using Classes| style="border: 2px solid black;" valign="top"| :[https://docs.python.org/3/tutorial/classes.html Classes]|- style="background-color:white;border:none;"| style="border: 2px solid black;"| Scope| style="border: 2px solid black;| [https://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/ Notes on Python Variable Scope]
|}
= INVESTIGATION 1: Classes and objects Objects =
In the last lab we created a '''class''' named Student and a couple of '''objects''' of type Student which were named student1 and student2. Let's spend some more time on that distinction.
|- style="background-color:white;border:none;"
| Car
| Andrew's silver Civic with VIN 1234567890, John's blue Subaru with VIN 0987654321, Evan's BWMMSeries Black BMWMSeries with VIN 4567981230
|}
You may find that when you have more than one or two objects of a type - it will be easier to store them in a Python list instead of having separate variables for each of them. Again - which makes more sense depends entirely on what you do with your objects in your application in any particular situation.
To get you started, here's a template for a file you should name '''lab7a.py'''
<source lang="python">
# Store information about different models from a specific manufacturer. Perhaps how many seats they have and how much fuel they use and the price range:
# Doesn't have to be BMW, pick any car or bike manufacturer:
class BWMModelBMWModel:
</source>
 
= INVESTIGATION 2: Scope =
 
Scope means where in the code a variable is accessible from, and how long that variable exists.
 
In this lab we will look at the types of scope you're most likely to run into and which are most likely to cause trouble for you. We will not be explicitly looking at class scope, nested functions, built-in variables, constants, and mutability.
 
== Local Scope ==
 
In Python any variable that is created inside a function has local scope. That means that it is accessible by any code inside that function, and is not accessible by any code outside the function. In other languages this concept is related to '''block''' scope but that does not exist in python. Every block inside a function has the same scope.
 
Try the following code. Have each in a separate Python file, iPython would cause way too many complications for this lab.
 
'''lab7b.py''' - local scope:
<source lang="python">
#!/usr/bin/env python3
 
def function1():
# This variable 'a' is completely unrelated to the variable 'a' in function2():
a = 'Andrew 1'
print(a)
 
def function2():
# This variable 'a' is completely unrelated to the variable 'a' in function1():
a = 'Andrew 2'
print(a)
 
# Note that you cannot make any of the following print() functions work because neither of
# the variables '''a''' exist in this scope (outside the function where they were defined):
a = 'Andrew 500'
print(a)
function1()
print(a)
function2()
print(a)
function1()
function2()
</source>
 
== Global Scope ==
 
Sometimes you want to have a variable accessible from anywhere in your program, including inside and outside any functions. Here's an example:
 
<source lang="python">
#!/usr/bin/env python3
 
def function1():
print(authorName)
 
def function2():
print(authorName)
 
authorName = 'Andrew'
print(authorName)
function1()
print(authorName)
function2()
print(authorName)
</source>
 
Note that the same thing is printed over and over because the '''authorName''' variable is defined outside a function which makes it global which makes it accessible from anywhere.
 
Python has one weird quirk when it comes to global scope: if you assign something to a variable inside a function - it will assume you want to create a new variable in that function's local scope. That will hide the global variable inside the function unless you declare it explicitly as global:
 
<source lang="python">
#!/usr/bin/env python3
 
def function1():
authorName = 'Andrew 1'
print(authorName)
 
def function2():
global authorName
authorName = 'Andrew 2'
print(authorName)
 
authorName = 'Andrew'
print(authorName)
function1()
print(authorName)
function2()
print(authorName)
</source>
 
Note that the function1() call does not modify the global '''authorName''' variable but function2() does.
 
As an exercise: modify the example above so that it would print the following, by changing only the scope of some variables. Save the program as '''lab7c.py''':
 
<source>
Andrew
Andrew 1
Andrew 1
Andrew 2
Andrew 1
</source>
 
== Object/Instance Scope ==
 
Every object can have variables that exist for that object only. You create and access those variables with the '''self.''' notation. Note that these are not '''class''' variables. Each object has its own set of '''instance''' variables. You will have seen that when you created objects in the Classes and Objects section above.
= LAB 7 SIGN-OFF (SHOW INSTRUCTOR) =
:'''Have Ready to Show Your Instructor:'''
::<span style="color:green;font-size:1.5em;">&#x2713;</span> Output of: <code>./CheckLab7.py -f -v</code>running your scripts for this lab and ::<span style="color:green;font-size:1.5em;">&#x2713;</span> Output of: <code>cat lab7a.py lab7b.py lab7c.py</code> 
= LAB REVIEW =
#
[[Category:OPS435-Python]]
1,760
edits

Navigation menu