Again: Please hear my plea: print without softspace
Martin Bless
m.bless at gmx.de
Wed Mar 3 05:32:34 EST 2004
["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.
Anybody any ideas?
#!/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