"print" as function not statement
Heather Coppersmith
me at privacy.net
Mon Mar 8 07:56:19 EST 2004
On 8 Mar 2004 08:31:48 GMT,
Duncan Booth <me at privacy.net> wrote:
> def show(*args, **kw):
> separator = kw.get('separator', ' ')
> trailer = kw.get('trailer', '\n')
> to = kw.get('to', sys.stdout)
> for keyword in kw:
> if not keyword in ('separator', 'trailer', 'to'):
> raise TypeError(
> 'show() got an unexpected keyword argument' + keyword)
> to.write(separator.join([ str(arg) for arg in args]))
> to.write(trailer)
> if len(args)==1:
> return args[0]
> else:
> return args
List comprehensions are shiny and new and theoretically sound, and
definitely make things more clearer (sic) in plenty of cases, but
this one seems gratuitous to this old-timer. What about:
to.write( separator.join( map( str, args ) ) )
I'm half-tempted to suggest that there be a string.join_as_strings
method (or something with a more concise name) like this:
class string:
# other stuff elided to conserve bandwidth
def join_as_strings( self, list_of_objects ):
self.join( map( str, list_of_objects ) )
but it's (obviously) easy enough to create as necessary, even as a
(gasp!) stand-alone function:
def join_as_strings( list_of_objects, separator = ' ' ):
separator.join( map( str, list_of_objects ) )
Regards,
Heather
--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
More information about the Python-list
mailing list