Changes

Jump to: navigation, search

Rchan sandbox

507 bytes added, 19:53, 3 November 2019
Investigation II - Objects and Methods
:4. You may also notice that when we called the print() function with our time object t1, the print function only showed that it is an Time object and its memory location, but did not display its properties (i.e. data attributes) like the values of its hour, minute, and second attributes.
:5. Try to find out how to test the valid_date(), change_time() functions to make sure they all work.
:6. Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">cd ~/ops435/lab7/pwd #confirm that you are in the right directoryls CheckLab7.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab7.pypython3 ./CheckLab7.py -f -v lab7d</source>:7. Before proceeding, make certain that you identify all errors in lab7d.py. When the checking script tells you everything is OK - proceed to the next step.
== Part 2 - Special object methods ==
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab7.py
python3 ./CheckLab7.py -f -v lab7dlab7e
</source>
:9. Before proceeding, make certain that you identify all errors in lab7a.py. When the checking script tells you everything is OK - proceed to the next step.
: Changing or specifying the behaviour of an operator so that it works with programmer-defined types is called '''operator overloading'''.
: Let's add the appropriate code to the __add__ function to overload the '+' operator so that we can use an arithmetic expression to tell the python interpreter to add two time object.
::* '''Associate the following code to the __add__ method for Time class in lab7d.py''':<source lang="python">
def __add__(self, t2):
return self.sum_times(t2)
</source>
:1. Copy lab7e.py to a new file called lab7f.py. Add the function definition for __add__() after the __str__() function in lab7dto lab7f.py. Make sure that the '''def __add__(self, t2):''' line has the same indentation level as the __init__() function.:2. Save the file lab7dlab7f.py and test it in an interactive Python shell:<source lang="bash">
[rchan@centos7 lab7]$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lab7d lab7f import *
>>> t1 = Time(9,50,0)
>>> t2 = Time(1,1,1)
>>> x = t1 + t2
>>> type(x)
<class 'lab7dlab7f.Time'>
>>> print(x)
10:51:01
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab7.py
python3 ./CheckLab7.py -f -v lab7dlab7f
</source>
:4. Before proceeding, make certain that you identify all errors in lab7a.py. When the checking script tells you everything is OK - proceed to the next step.
1,760
edits

Navigation menu