[Tutor] Surprised that print("a" "b") gives "ab"

Ben Finney ben+python at benfinney.id.au
Sun Mar 6 01:37:44 EST 2016


boB Stepp <robertvstepp at gmail.com> writes:

> […] why was this feature implemented?

You've been asking that a lot lately :-) This forum is unlikely to be
able to give authoritative answers to “why was the language designed the
way it is?”.

We are more able to explain what justification there is for a behaviour
remaining as it is. That's a different question, though.

> Is there a use case where it is more desirable to not have a string
> concatenation operator explicitly used? The only thing that comes to
> my mind are strings spread over multiple lines, say
>
> print("This will be a ..."
>     "...very long string...")
>
> But is this preferable to
>
> print("This will be a ..." +
>     "...very long string...")
>
> ?  I personally prefer the latter, so I am probably missing something.

It is salient to this issue, that the two cases above are semantically
different.

The first one is defined (by the syntax for literals) to create a
*single* string object. Semantically, the fragments are specifying one
object in a single step.

The second is semantically (i.e. by the semantics of how such
expressions are defined to work) creating two distinct objects, then
creating a third using an operation, then discarding the first two.

For me, that makes a big difference. When strings need to be split over
several lines, it is convenient to be able to express directly in the
code “these fragments are intended to be all part of the same object”.
And it helps to see that in other people's code, too.

I am sympathetic to the small visible difference between your two
examples, and I don't deny that others may not find this feature as
convenient or elegant as I do.

-- 
 \           “The long-term solution to mountains of waste is not more |
  `\      landfill sites but fewer shopping centres.” —Clive Hamilton, |
_o__)                                                _Affluenza_, 2005 |
Ben Finney



More information about the Tutor mailing list