[Tutor] mistaken about splitting expressions over lines
Peter Otten
__peter__ at web.de
Tue Jun 25 16:11:44 CEST 2013
Albert-Jan Roskam wrote:
> _______________________________
>>From: eryksun <eryksun at gmail.com>
>>To: Jim Mooney <cybervigilante at gmail.com>
>>Cc: tutor at python.org
>>Sent: Tuesday, June 25, 2013 2:14 PM
>>Subject: Re: [Tutor] mistaken about splitting expressions over lines
>
> <snip>
>
>>
>>>>> a = ('this' # this way
>>... ' string' ' is long') # is more flexible
>>>>> a
>>'this string is long'
>
>
> I did something similar after having read
> http://docs.python.org/2/howto/doanddont.html, under "Using Backslash to
> Continue Statements".
>
> But I always use + signs. I didn't know that omitting them also works. Is
> str.__add__ called then, too? Isn't this a violation of the 'Explicit is
> better than implicit(ly concatenate strings)' principle?
>>>> a = ('this' +
> ' string' +
> ' is long')
>>>> a
> 'this string is long'
In older Pythons for ("alpha" "beta") the compiler would merge the two
strings into one whereas ("alpha" + "beta") would trigger a str.__add__()
call at runtime. Nowadays the peephole optimiser recognizes ("alpha" +
"beta") and replaces it with a single string:
>>> import dis
>>> def f():
... return ("alpha" +
... "beta")
...
>>> dis.dis(f)
3 0 LOAD_CONST 3 ('alphabeta')
3 RETURN_VALUE
More information about the Tutor
mailing list