[Tutor] REPL format

Danny Yoo dyoo at hashcollision.org
Sun Apr 26 02:38:58 CEST 2015


On Sat, Apr 25, 2015 at 4:38 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> I'm curious why, when I read and decode a binary file from the net in one
> fell swoop, the REPL prints it between parentheses, line by line but with
> no commas, like a defective tuple.


The REPL is trying to be nice here.  What you're seeing is a
representation that's using a little-known Python syntactic feature:
string literals can be spread across lines.  See:

    https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation

At the program's parse time, the Python compiler will join adjacent
string literals automatically.


It's a cute-but-nasty trick that some other languages do, such as C++.


I would strongly discourage not using it yourself in your own
programs: it's the source of a very common mistake.  Here's an
example:

####################
def f(x, y):
    print(x)
    print(y)

f("hello"
  "world")
####################

What do you expect to see?  What do you see?

So that's why I don't like this feature: makes it really hard to catch
mistakes when one is passing string literals as arguments and forgets
the comma.  Especially nasty when the function being called uses
optional arguments.


But I'm surprised, frankly, that you're seeing the standard Python
REPL using string literal concatenation, as in my memory, it was a lot
less accommodating.  Is this Python 3?


More information about the Tutor mailing list