Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75098

Where is the function of Repr.repr1() in this example?

X-Received by 10.236.209.97 with SMTP id r61mr806977yho.33.1406151452580; Wed, 23 Jul 2014 14:37:32 -0700 (PDT)
X-Received by 10.140.30.52 with SMTP id c49mr63026qgc.7.1406151452566; Wed, 23 Jul 2014 14:37:32 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!j15no2205910qaq.0!news-out.google.com!j6ni12934qas.0!nntp.google.com!v10no2139902qac.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 23 Jul 2014 14:37:32 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=64.229.140.25; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo
NNTP-Posting-Host 64.229.140.25
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <09fdc136-adc2-4dfb-af7b-8f36df82d479@googlegroups.com> (permalink)
Subject Where is the function of Repr.repr1() in this example?
From fl <rxjwg98@gmail.com>
Injection-Date Wed, 23 Jul 2014 21:37:32 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 2339
X-Received-Body-CRC 1983171824
Xref csiph.com comp.lang.python:75098

Show key headers only | View raw


Hi,
I run the example code below from website: 

https://docs.python.org/2/library/repr.html#repr.Repr.repr1

If I run these lines from an editor, it echoes:

>>> <open file '<...at 0x01EF4020>
>>> dsfdsf         # entered letters


If I only run the last line (hoping the same effect with running from the editor)
it simply echoes:

>>> print aRepr.repr(sys.stdin)
<open file '<...at 0x01EF4020>
>>> 


I have these questions about this example code:

1. What purpose of the author wants from the example code? 
2. I do not see Repr.repr1() in the code snippet. Why did the author mention
that first?
3. Why is it different from running the last line directly in the Interactive 
Window, and within an editor file?


Thanks,



..................
The use of dynamic dispatching by Repr.repr1() allows subclasses of Repr to add 
support for additional built-in object types or to modify the handling of types
already supported. This example shows how special support for file objects could
be added:

import repr as reprlib
import sys

class MyRepr(reprlib.Repr):
    def repr_file(self, obj, level):
        if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
            return obj.name
        else:
            return repr(obj)

aRepr = MyRepr()
print aRepr.repr(sys.stdin)          # prints '<stdin>'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Where is the function of Repr.repr1() in this example? fl <rxjwg98@gmail.com> - 2014-07-23 14:37 -0700

csiph-web