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

Ben Finney ben+python at benfinney.id.au
Sun Mar 6 02:30:14 EST 2016


boB Stepp <robertvstepp at gmail.com> writes:

> Under the hood, are the two approaches above done in the same way?  I
> get your semantics point, but are there two string objects created in
> both approaches or does the first in fact create only a single object?

Sometimes, it does. That's an implementation detail, and the behaviour
is not guaranteed and can change at any time, even within the exact same
program. So it's not something worth considering when writing or reading
that code for semantics.

> If the first truly only creates a single object, then it seems that
> this is a more efficient approach.

That's the danger. Consider efficiency of the implementation to be not
your job, when learning the language. Far more important are clear
expression of intent, and ease of finding errors.

-- 
 \        “You can't have everything; where would you put it?” —Steven |
  `\                                                            Wright |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list