Albert-Jan Roskam wrote: > _______________________________ >>From: eryksun <eryk...@gmail.com> >>To: Jim Mooney <cybervigila...@gmail.com> >>Cc: tutor@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 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor