Changes

Jump to: navigation, search

Rchan sandbox

2,215 bytes added, 16:45, 3 November 2019
Investigation II - Objects and Methods
>>>
</source>
:3. Please study the output of the interactive shell session above and 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 You may also notice that when we called the print() function with our time objectt1, the print function only able to show showed that it is an Time objectand its memory location, but unable to did not display its properties (i.e. data attributes) like the values of its hour, minute, and second attributes.
== Part 2 - Special object methods ==
: Attached Each programmer-defined object has a few special methods which can be used to manipulate the object, the one we already know is the '''__init__''' method, which python refers it as the object constructor, and we can associate code to this method in the class definition.: In this part, we are going to investigate and study the '''__str__''' and '''__repr__''' special methods.::'''Associate the following code to the __str__''' method for the Time class in lab7d.py: <source lang="python"> def __str__(self): '''return a string representation for the object methodsself''' return '%.2d:%2d:%2d' % (self.hour, self.minute, self.second) </source>::1. Add the function definition for __str__() after the __init__() function in lab7d.py. Make sure that the '''def __str__(self):''' line has the same indentation level as the __init__() function.::2. Save the file lab7d.py and test it 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 lab7d1 import *>>> t1 = Time(9,50,0)>>> t1<lab7d1.Time object at 0x7f830b498be0>>>> print(t1)09.50.00>>> t1.format_time()'09:50:00'>>> print(t1.format_time())09:50:00>>> </source>:: 3. Now with the proper code (same code for the format_time() function) attached to the __str__methon, we have a string representation for our time object that the print() function can/will use.::4. You will still notice that typing the time object name itself on an interactive python shell, type Python interpreter will just display the type of the object and its location in memory. ::5. Let's look at the next special object method '''__repr__()'''. We can attached code to this function to tell the python interpreter what we want the object to appear on an interactive shell.::6. '''Associate the following code to the __repr__''' method for the Time class in lab7d.py: <source lang="python"> def __repr__(self): '''return a string representation for the object self''' return '%.2d.%2d.%2d' % (self.hour, self.minute, self.second) </source> 
== Part 3 - Operator overloading ==
: Attached code to operator '+' and '-' to support the operation of adding two time object.
1,760
edits

Navigation menu