Building a multiline string
Steve Holden
steve at holdenweb.com
Thu Feb 4 07:31:01 EST 2010
lallous wrote:
> Hello
>
> Maybe that's already documented, but it seems the parser accepts to
> build a long string w/o really using the first method:
>
> # Method1
> x = "line1" + \ # cannot use comments!
> "line2"+ \
> "line3"
>
> and instead using a list with one element like this:
>
> # Method2
> x = [
> "line1" # can use comments
> "line2"
> "line3"
> ][0]
>
> Or:
> # Method3
> x = (
> "line1" # can use comments
> "line2"
> "line3"
> )
>
> (Not that I don't want new lines in the strings)
>
> Now should I be using method 2 or 3 in production code?
>
I should have thought it was pretty obvious that method 2 creates a list
and then performs an indexing operation on it. These are completely
unnecessary operations, which are avoided in method 3 which is a simple
parenthesised expression.
So why anyone would want to adopt method 2, which is also mess clear as
source code, is beyond me.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
More information about the Python-list
mailing list