[Tutor] method to print in interpreter
Remco Gerlich
scarblac@pino.selwerd.nl
Fri, 22 Jun 2001 01:09:38 +0200
On 0, Christopher Smith <csmith@blakeschool.org> wrote:
> Dear List,
>
> I read about defining __str__ in a class so the interpreter will know how
> to process a print command but I have not yet found what I must do to be
> able to get it to print if, in the interpreter, you don't use a "print"
> command. i.e. How can I get the evaluation itself to print instead of
> giving the address of the instance. Here's a run to demonstrate what I
> mean.
>
> >>> from my import point
> >>> p=point(1,2)
> >>> print p
> (1,2)
> >>> p
> <my.point instance at 0x00c448b0>
> >>>
>
> I would like ">>> p" to return something like '(1,2)', too.
This uses the repr() of the object, not its str(). You need to define
__repr__().
> Also, in the Runtime Services section of the library documentation, I
> see "cpickle" and "operator" listed and I find "test_cpickle.py" and
> "test_operator.py" on my disk, but I don't see the "cpickle.py" or
> "operator.py" anywhere. Where are they?
They're written in C, not Python (that's what the c in CPickle means, it's
the faster equivalent of the Pickle module).
See Modules/cPickle.c and Modules/operator.c in the source distribution.
cPickle is nice C code, but operator.c is ugly, apparently just wrappers
around other interpreter functions.
--
Remco Gerlich