Differences between revisions 1 and 2
Revision 1 as of 2005-09-03 01:32:31
Size: 281
Editor: NickCoghlan
Comment:
Revision 2 as of 2005-09-03 01:35:07
Size: 1471
Editor: NickCoghlan
Comment: Add the output function
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

{{{#!python
def output(*args, **kwds):
    """Functional replacement for the print statement

    >>> output(1, 2, 3)
    1 2 3
    >>> output(1, 2, 3, sep='')
    123
    >>> output(1, 2, 3, sep=', ')
    1, 2, 3
    >>> output(1, 2, 3, end='Alternate line ending')
    1 2 3Alternate line ending
    >>> import sys
    >>> output(1, 2, 3, stream=sys.stderr)
    1 2 3
    >>> output(*range(10))
    0 1 2 3 4 5 6 7 8 9
    >>> output(*(x*x for x in range(10)))
    0 1 4 9 16 25 36 49 64 81
    """
    # Parse the keyword-only optional arguments
    defaults = {
        "sep": " ",
        "end": "\n",
        "stream": sys.stdout,
    }
    for name, default in defaults.items():
        item = None
        try:
            item = kwds[name]
        except KeyError:
            pass
        if item is None:
            kwds[name] = default
    sep, end, stream = kwds["sep"], kwds["end"], kwds["stream"]
    # Perform the print operation without building the whole string
    for arg in args[:1]:
        stream.write(str(arg))
    for arg in args[1:]:
        stream.write(sep)
        stream.write(str(arg))
    stream.write(end)
}}}

This page discusses the benefits of replacing the current print statement with an equivalent builtin. The output function presented below does everything the print statement does without requiring an hacking of the grammar, and also makes a number of things significantly easier.

   1 def output(*args, **kwds):
   2     """Functional replacement for the print statement
   3 
   4     >>> output(1, 2, 3)
   5     1 2 3
   6     >>> output(1, 2, 3, sep='')
   7     123
   8     >>> output(1, 2, 3, sep=', ')
   9     1, 2, 3
  10     >>> output(1, 2, 3, end='Alternate line ending')
  11     1 2 3Alternate line ending
  12     >>> import sys
  13     >>> output(1, 2, 3, stream=sys.stderr)
  14     1 2 3
  15     >>> output(*range(10))
  16     0 1 2 3 4 5 6 7 8 9
  17     >>> output(*(x*x for x in range(10)))
  18     0 1 4 9 16 25 36 49 64 81
  19     """
  20     # Parse the keyword-only optional arguments
  21     defaults = {
  22         "sep": " ",
  23         "end": "\n",
  24         "stream": sys.stdout,
  25     }
  26     for name, default in defaults.items():
  27         item = None
  28         try:
  29             item = kwds[name]
  30         except KeyError:
  31             pass
  32         if item is None:
  33             kwds[name] = default
  34     sep, end, stream = kwds["sep"], kwds["end"], kwds["stream"]
  35     # Perform the print operation without building the whole string
  36     for arg in args[:1]:
  37         stream.write(str(arg))
  38     for arg in args[1:]:
  39         stream.write(sep)
  40         stream.write(str(arg))
  41     stream.write(end)

PrintAsFunction (last edited 2011-08-14 09:25:04 by eth595)

Unable to edit the page? See the FrontPage for instructions.