Open main menu

CDOT Wiki β

Changes

Rchan sandbox

24 bytes added, 19:58, 3 November 2019
Part 2 - Special object methods
: 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 self'''
return '%.2d:%2d:%2d' % (self.hour, self.minute, self.second)
</source>
:1. Make a copy of lab7d.py and name it as lab7e.py. Add the function definition for __str__() after the __init__() function in lab7dlab7e.py. Make sure that the '''def __str__(self):''' line has the same indentation level as the __init__() function.:2. Save the file lab7dlab7e.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 lab7e import *
>>> t1 = Time(9,50,0)
>>> t1
<lab7dlab7e.Time object at 0x7f830b498be0>
>>> print(t1)
09.50.00
: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, the 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 also attached code to this function to tell the python interpreter what we would like the object to look like on in an interactive shell.::* '''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'''
</source>
:6. Add the function definition for __repr__() after the __str__() function in lab7d.py. Please note that we use the '.' instead of ':' in the formatting string. Make sure that the '''def __repr__(self):''' line has the same indentation level as the __init__() function.
:7. Save the file lab7dlab7e.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 lab7d2 lab7e import *
>>> t1 = Time(9, 50, 0)
>>> t1
python3 ./CheckLab7.py -f -v lab7e
</source>
:9. Before proceeding, make certain that you identify all errors in lab7alab7e.py. When the checking script tells you everything is OK - proceed to the next step.
== Part 3 - Operator overloading ==
1,760
edits