Again: Please hear my plea: print without softspace

David MacQuigg dmq at gain.com
Wed Mar 3 12:47:04 EST 2004


On Wed, 03 Mar 2004 10:32:34 GMT, m.bless at gmx.de (Martin Bless) wrote:

>["Rainer Deyke" <rainerd at eldwood.com>]
>
>>but only because 'sys.stdout.write' is too long to type all
>>the time, and never with more than one argument.
>
>Yes, I feel it's a pity that the standard write method of file objects
>doesn't take multiple arguments. Would take some of the pressure.
>
>One thing I can do since Python-2.2 is to inherit from 'file' and add
>for example a 'writem' (write many, write multiple) method. This does
>work.
>
>Unfortunately it seems I can't add that 'writem' method to sys.stdout,
>as the following shows.

Why do you want to do this, when it is so easy to define your own
class to do exactly what you want (as in your example below)?

>Anybody any ideas?

I think Ruby has what you need.  The default print method requires an
explicit "\n" on every line.  To me, this makes normal code look ugly,
but your preference is valid too.

Ruby also allows you to redefine its built-in classes, so if you need
that level of flexibility, this language may be a better choice.

Lua is also a possibility, but I know even less of that language.

Python differs from other languages in that it tries to keep a common
core language that is the same for everyone, while still allowing
extensions like your example below.  The arguments against providing
more flexibility (e.g. a builtin macro language) are strong.  See for
example:
http://groups.google.com/groups?selm=bhqr78021hp%40enews1.newsguy.com

To add a variation of the print statement to the Python language, you
would need to demonstrate a real need for the change, not just a
personal preference for leaving out the " ".  Generalizing the
'sys.stdout.write()' method to include multiple arguments might be
more acceptable.  If you do a PEP along these lines, I would
emphasize:
- does not break existing code
- some good examples where this would have an advantage over current
best practices.  Your choice of integer arguments in the example below
is wise.  Had it been strings, the current solution would be to just
put '+' between the pieces instead of ','

-- Dave

>#!/usr/bin/env python
># -*- coding: iso-8859-1 -*-
>"""Test file object."""
>
>__author__ = "Martin Bless, 2004-03-03"
>
>if 1 and "this does work":
>    class MyFile(file):
>
>        def writem(self,*args):
>            "Method to write many arguments."
>            for arg in args:
>                self.write(str(arg))
>
>    f2name = 'xxxtempfile'
>    f2 = MyFile(f2name,'w')
>    f2.writem(1,2,3,4)
>    f2.close()
>
>
>if 1 and "this does not work with sys.stdout":
>    """
>    Attempt to add a method to a class at runtime.
>    See Python Cookbook or ASPN,
>    Object-Oriented Programming, Brett Cannon.
>    """
>    import sys
>    def writem(self,*args):
>        "Function to be turned into a method.."
>        for arg in args:
>            self.write(str(arg))
>    # will not work
>    setattr(sys.stdout.__class__,'writem',writem)
>
>    """The above 'setattr' will give this error:
>    Traceback (most recent call last):
>      File "test_file_object.py", line 33, in ?
>        setattr(sys.stdout.__class__,'writem',writem)
>    TypeError: can't set attributes of built-in/extension type 'file'
>    """
>
>mb - Martin Bless




More information about the Python-list mailing list