Changes

Jump to: navigation, search

Rchan sandbox

1,250 bytes added, 15:15, 3 November 2019
Part 1 - Classes and Methods for our Time objects
</source>
: Please notice that the function named sec_to_time() did not get moved under the class block. It remains as an external function.
:1. Create a new python file and name it as '''lab7d.py''' and place the code above in it.:2. Save the file, and test the new time object in an interactive Python shell: <source lang="bash">[rchan@centos7 lab7]$ python3Python 3.4.9 (default, Aug 14 2018, 21:28:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> from lab7d import *>>> t1 = Time(9,50,0)>>> t1<lab7d.Time object at 0x7f1232a79be0>>>> print(t1)<lab7d.Time object at 0x7f1232a79be0>>>> format_time(t1)Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'format_time' is not defined>>> Time.format_time(t1)'09:50:00'>>> type(Time.format_time)<class 'function'>>>> t1.format_time()'09:50:00'>>> type(t1.format_time)<class 'method'>>>> </source>:3. Note that in order to call the format_time() function, we have to prefix it with the class name '''Time''' as it is under the class definition in lab7d.py. Also note that format_time() is now a method of the time object '''t1''.:4. Also note that when we called the print() function with our time object, the print function only able to show that it is an Time object, but unable to display its properties (i.e. data attributes) like the values of its hour, minute, and second attributes.
== Part 2 - Special object methods ==
1,760
edits

Navigation menu