Py 3.0 print

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bearophileHUGS@lycos.com

    #1

    Py 3.0 print

    There is/was a long discussion about the replacement for print in
    Python 3.0 (I don't know if this discussion is finished):


    There is also a wiki page that collects the ideas:


    There is the will to keep the printing operation very simple for the
    most common situation, but at the same time to make it flexible
    (optional no space between printed items, newline, redirection to file,
    etc), such requirements can collide. Another problem is to avoid
    backward compatibility problems in the source code. I post my idea here
    because I think I'm not fit for the python-dev mailing list yet. I hope
    posting this here is okay.

    A printing operation as a function requires the (), but I think there
    can be little advantages (but not many advantages, the print statement
    is acceptable for me still), so I agree to make it a function.

    Name: I think print() and printnl() can be fine names for the two
    functions, but this can give problems, so other names can be:
    write/writenl, show/shownl, echo/echonl, put/putnl, emit/emitnl,
    say/saynl
    I think the write/writenl or show/shownl namas are the best (writeln
    instead of writenl is acceptable too, I think).

    Spacing problem: I can solve this problem with the solution used in the
    Pascal language, that is to not print the space between items. Here are
    some examples, from the PrintAsFunction page:

    # Standard printing
    print 1, 2, 3
    shownl(1, " ", 2, " ", 3)

    # Printing without any spaces
    print "%d%d%d" % (1, 2, 3)
    shownl(1, 2, 3)

    # Print as comma separated list
    print "%d, %d, %d" % (1, 2, 3)
    shownl(1, ", ", 2, ", ", 3)

    # Print without a trailing newline
    print 1, 2, 3,
    show(1, " ", 2, " ", 3)

    # Print to a different stream
    print >> sys.stderr, 1, 2, 3
    shownl(1, " ", 2, " ", 3, to=sys.stderr)

    # Print a simple sequence
    print " ".join(map( str, range(10)))
    shownl( " ".join(map( str, range(10))) )

    # Print a generator expression
    print " ".join(str( x*x) for x in range(10))
    shownl( " ".join(str( x*x) for x in range(10)) )


    It's not very nice looking, but it's simple, and it avoids problems and
    the use of the "sep" parameter. This is the alternative that uses the
    sep:

    # Print without a trailing newline
    print 1, 2, 3,
    show(1, 2, 3)

    # Printing without any spaces
    print "%d%d%d" % (1, 2, 3)
    shownl(1, 2, 3, sep='')

    # Print as comma separated list
    print "%d, %d, %d" % (1, 2, 3)
    shownl(1, 2, 3, sep=', ')

    # Print a simple sequence
    print " ".join(map( str, range(10)))
    shownl(*range(1 0))


    I think the % string formatting used in Python can be fine for the C
    language (and ), but I have to look its syntax each time I use it ("%"
    character + Mapping key (optional) + Conversion flags (optional) +
    Minimum field width (optional) + Precision (optional) + Length modifier
    (optional) + Conversion type), and I think a simpler solution (fit for
    the most common usage) can be found for a high level langage like
    Python (I can say something similar about the syntax to define the base
    of integer numbers, the use of the trailing 0 for the octals isn't
    good).

    Bye,
    bearophile

Working...