[Python-Dev] New Wiki page - PrintAsFunction (original) (raw)
Ron Adam rrr at ronadam.com
Sat Sep 3 10:28:33 CEST 2005
- Previous message: [Python-Dev] New Wiki page - PrintAsFunction
- Next message: [Python-Dev] New Wiki page - PrintAsFunction
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Nick Coghlan wrote:
All,
I put up a Wiki page for the idea of replacing the print statement with an easier to use builtin: http://wiki.python.org/moin/PrintAsFunction Cheers, Nick.
Looks like a good start, much better than just expressing opinions. :-)
How about making it a class?
There are several advantages such as persistent separators and being able to have several different instances active at once.
Cheers, Ron
import sys class Print(object): newline = '\n' sep = ' ' def init(self, out=sys.stdout): self.out = out def call(self, *args, **kwds): savesep = self.sep try: self.sep = kwds['sep'] except KeyError: pass for arg in args[:1]: self.out.write(str(arg)) for arg in args[1:]: self.out.write(self.sep) self.out.write(str(arg)) self.sep = savesep def ln(self, *args, **kwds): self(*args, **kwds) self.out.write(self.newline)
default "builtin" instance
write = Print() # could be print in place of write in python 3k.
standard printing
write.ln(1, 2, 3)
print without spaces
write.ln(1, 2, 3, sep='')
print comma separated
write.ln(1, 2, 3, sep=', ')
or
write.sep = ', ' # remain until changed write.ln(1, 2, 3) write.ln(4, 5, 6) write.sep = ' '
print without trailing newline
write(1, 2, 3)
print to a different stream
printerr = Print(sys.stderr) printerr.ln(1, 2, 3)
print a simple sequence
write.ln(*range(10))
Print a generator expression
write.ln((xx for x in range(10)))
print to file
f = open('printout.txt','w') fileprint = Print(f) fileprint("hello world\n") f.close()
- Previous message: [Python-Dev] New Wiki page - PrintAsFunction
- Next message: [Python-Dev] New Wiki page - PrintAsFunction
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]