Open main menu

CDOT Wiki β

Changes

Rchan sandbox

184 bytes added, 12:09, 3 November 2019
Investigation II
<br><br>
=Investigation II=
: In the previous investigation, the functions that were defined for manipulating our time object are not tied directly to our time object. Given our time object alone, we won't be able to tell that there exist a function called sum_times() which can be used to add two time objects and return their sum.
To tie up those functions to our time objects, we only need to move those functions definition under the class block which define our Time object.
== Part 1 - Classes and Methods for our Time objects ==
: The following illustration shows how we can change external functions to become object methods (aka class functions) for our Time object. It is simply by moving the function definitions to be under the class definition for the Time object:<source lang="python">
#!/usr/bin/env python3
# Student ID: rchan
return time
</source>
: Please notice that the function named sec_to_time() did not get movedunder the class block. It remains as an external function.
:1. Create a new file and name it as '''lab7d.py''' and place the code above in it.
== Part 2 - Special object methods ==
: Attached code to special object methods: __str__, and __repr__
== Part 3 - Operator overloading ==
: Attached code to operator '+' and '-' to support the operation of adding two time object.
= Investigation III - Scope =
1,760
edits