| Store | Cart

"print" as function not statement

From: Heather Coppersmith <m...@privacy.net>
08 Mar 2004 07:56:19 -0500
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

Recent Messages in this Thread
Mark Kamikaze Hughes Mar 08, 2004 01:36 am
John Roth Mar 08, 2004 02:38 am
Leif K-Brooks Mar 08, 2004 07:32 am
Duncan Booth Mar 08, 2004 08:31 am
Leif K-Brooks Mar 08, 2004 10:36 am
A.M. Kuchling Mar 08, 2004 01:29 pm
Heather Coppersmith Mar 08, 2004 12:56 pm
David MacQuigg Mar 09, 2004 09:04 pm
Aahz Mar 10, 2004 10:43 am
Peter Otten Mar 10, 2004 12:52 pm
David MacQuigg Mar 10, 2004 03:32 pm
Messages in this thread