Newbie: Looking for code review on my first Python project.
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jan 10 23:24:21 EST 2012
On Tue, 10 Jan 2012 22:59:23 -0500, Terry Reedy wrote:
> On 1/10/2012 8:43 PM, Chris Angelico wrote:
>
>> about = "Built by Walter Hurry using Python and wxPython,\n" + \
>> "with wxGlade to generate the code for the GUI elements.\n" +
>> \ "Phil Lewis' get_iplayer does the real work.\n\n" + \
>> "Version 1.05: January 10, 2012"
>>
>> I'd do this with a triple-quoted string:
>>
>> about = """Built by Walter Hurry using Python and wxPython, with
>> wxGlade to generate the code for the GUI elements. Phil Lewis'
>> get_iplayer does the real work.
>
> I would too, but if you prefer the indentation, just leave out the '+'s
> and let Python do the catenation when compiling:
> >>> s = "abc\n" \
> "def\n"\
> "ghi"
> >>> s
> 'abc\ndef\nghi'
Actually, in recent Pythons (2.5 or better, I believe) the peephole
optimizer will do the concatenation at compile time even if you leave the
+ signs in, provided that the parts are all literals.
>>> from dis import dis
>>> dis(compile(r's = "a\n" + "b\n"', '', 'single'))
1 0 LOAD_CONST 3 ('a\nb\n')
3 STORE_NAME 0 (s)
6 LOAD_CONST 2 (None)
9 RETURN_VALUE
--
Steven
More information about the Python-list
mailing list