'generating' a print statement

David Eppstein eppstein at ics.uci.edu
Thu May 8 01:20:39 EDT 2003


In article <qplua.7846$WA3.6988 at nwrddc03.gnilink.net>,
 "David Broadwell" <anti-spam.dbroadwell at mindspring.com> wrote:

> Do i have the wrong approach here?
> 
> using
> def wideprint(temp):
>     format = '"
>     format2 = '('
>     for i in range(len(temp)):
>         format = format + "%s   "
>         format2 = format2 + "temp["+str(i)+"],"
>     format = format + '"'
>     format2 = format2 + ')'
>     print "wideprint calling print with %s - %s " % (format, format2)
>     print format % format2

Is this some kind of anti-Perl joke?  You're building up strings that 
look like expressions, and you forgot to convert back to actual 
expressions at the end, I think you want
    print eval(format1) % eval(format2)

You also left off the close quote on line two.

But really it would be easier to do
    print "  ".join(temp)
(or print "  ".join(temp)+"  " if you really need the trailing blanks)
and skip all the format cruft.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list