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

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


Ben Finney <ben+python at benfinney.id.au> writes:

> 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.

Because I know there are resident pedants who love to get into the
details: I know that's not exactly how it works internally, and it
doesn't matter.

I'm not talking about what bytecode is produced, I'm talking about the
semantics of what the language reference defines: ‘+’ implies that a new
object will be created by calling ‘__add__’ or ‘__radd__’.

What the optimiser decides to omit is not relevant to that point: the
semantics of the ‘+’ operator imples that a third object will result
from combining two other objects.

-- 
 \       Moriarty: “Forty thousand million billion dollars? That money |
  `\            must be worth a fortune!” —The Goon Show, _The Sale of |
_o__)                                                       Manhattan_ |
Ben Finney



More information about the Tutor mailing list