PEP 259: Revise to remove context-driven magic from print

Bengt Richter bokr at accessone.com
Thu Jun 14 21:07:58 CEST 2001


On Thu, 14 Jun 2001 18:37:32 +0200, "Alex Martelli"
<aleaxit at yahoo.com> wrote:

>"David LeBlanc" <whisper at oz.nospamnet> wrote in message
>news:9gamor$8ft$6 at 216.39.170.247...
>    ...
>> makes sense to me. I also liked someone's idea of __print__ - if that
>
>The 'someone' was me ("was I"...?).
>
>> idea was in respect of giving objects the power to (as it where) print
>> themselves (return their string representation - so call it asstring()).
>
>How would that differ from the existing __str__ and __repr__ methods?
>
>No, __print__ would rather underlie the print statement just like
>__import__ underlies the import (and from) statements.  But I guess
>I gotta write a PEP about it...

Another bit of grist for the PEP 259 mill:

If you thought of

    print arg1,arg2

as implicitly doing

    print __builtins__.defaultformat % (arg1,arg2)

and borrowed an old idea from Fortran re format specs, so you
could handle "as many as necessary" in a format, e.g.,

    print "%(%s )\n" % (arg1,arg2)

would generate as many "%s " (note trailing space, if that's
what you want) as necessary. I might prefer

    print "%s%( %s)\n"

Other handy uses would shorten specific-length repeated patterns

    print "This is an 8-col table:\n--\n%4(%6s: %7.3f   )\n--\n" ...

With an appropriate place to store print's default format, one could
override at will.

With the above, any special behaviour would then be a matter of
tweaking format specification/interpretation instead of messing
with print itself.





More information about the Python-list mailing list