1,760
edits
Changes
→Investigation II
=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 ==The following illustration show how it should to change external functions to object methods (aka class functions). It is simply by moving the function definition to be doneunder the class definition for the new type of object:
<source lang="python">
#!/usr/bin/env python3
return time
</source>
Please notice that the function named sec_to_time() did not get moved. It remains an external function.
:1. Create a new file and name it as '''lab7d.py''' and place the code above in it.