[Python-Dev] New Wiki page - PrintAsFunction (original) (raw)
Vincenzo Di Massa hawk78_it at yahoo.it
Mon Sep 5 00:38:23 CEST 2005
- Previous message: [Python-Dev] New Wiki page - PrintAsFunction
- Next message: [Python-Dev] Asynchronous use of Traceback objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello, This is my first post here. I like python a lot: great job people! Thank you!
Alle 10:28, sabato 03 settembre 2005, Ron Adam ha scritto:
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?
I like the object idea, really a lot!
There are several advantages such as persistent separators and being able to have several different instances active at once. Cheers, Ron
I think savesep is unusefull.
import sys class Print(object): newline = '\n' sep = ' ' def init(self, out=sys.stdout, println=""): self.out = out self._print=self.printNOln
def __call__(self, *args, **kwds):
self._print(*args, **kwds)
def printNOln(self, *args, **kwds):
try:
sep = kwds['sep']
except KeyError:
sep = self.sep
for arg in args[:1]:
self.out.write(str(arg))
for arg in args[1:]:
self.out.write(sep)
self.out.write(str(arg))
def println(self, *args, **kwds):
self.printNOln(*args, **kwds)
self.out.write(self.newline)
# default "builtin" instance write = Print() # could be print in place of write in python 3k.
write._print=write.println # standard printing write(1, 2, 3) # print without spaces write(1, 2, 3, sep='') # print comma separated write(1, 2, 3, sep=', ') # or write.sep = ', ' # remain until changed write(1, 2, 3) write(4, 5, 6) write.sep = ' ' # print without trailing newline write._print=write.printNOln write(1, 2, 3) # print to a different stream printerr = Print(sys.stderr) printerr._print=write.println printerr(1, 2, 3) # print a simple sequence write._print=write.println write(range(10)) # Print a generator expression write((x*x for x in range(10))) # print to file f = open('printout.txt','w') fileprint = Print(f) fileprint("hello world\n") f.close()
Does this look good?
Ciao Vincenzo
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/hawk78it%40yahoo.it
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB http://mail.yahoo.it
- Previous message: [Python-Dev] New Wiki page - PrintAsFunction
- Next message: [Python-Dev] Asynchronous use of Traceback objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]