[Tutor] Interpreter pasting Question

Avi Gross avigross at verizon.net
Fri Dec 28 00:58:00 EST 2018


This is a serious question. I have tried things and searched and remain
stumped. It is about python and perhaps just the interpreter.

 

Copying and pasting multiple lines into the interpreter fails in mysterious
ways, unless they are a logical single entity.

 

Is there a way to change this behavior, or perhaps an editor/environment
that feeds multiple lines more carefully to the interpreter?

 

I am used to using R Studio and other tools with R. This is not about R but
please permit me an example.

 

I like to be able to tell it in the EDIT window that I want a single line or
a group of lines to be run on the console. That is especially useful when I
am debugging and want to run some code and go to the console and examine
things then continue. I may want to run one line or a region. Clearly
indentation is not an issue there.

 

The python interpreter I use (cpython 3.7.0) on windows lets me copy/paste a
simple single line of code. If it is a complex construct like an if/else
that is syntactically seen as a single statement, it allows it to be copied
in as a unit.

 

But something as simple as this:

 

X=5

Y=6

 

When copied in looks like this:

 

>>> X=5

Y=6

 

SyntaxError: multiple statements found while compiling a single statement

 

Yet I have seen programs that build up a string and give that to eval. Can
those be multiple statements? My tests using eval confuse me. But things I
can type in to the interpreter could be the same after a paste but perhaps
there are reasons they can't be?

 

It may be something as silly as python being too fast and reading all the
input. When I type and hit CARRIAGE RETURN/ENTER it may finish evaluating
before I even start typing the next line. 

 

The current behavior makes it hard to copy code from something like an email
message. Yes, I can (and do) insert the code in a file using IDLE or other
tools then ask to run the file but that is far from the same. Having to copy
one line at a time is beyond frustrating for me.

 

Feel free to tell me I am doing it wrong or of a way that works for others. 

 

Python has an eval() and an exec() and I would assume the latter would be a
way to see what works.

 

Here are three lines using \n:

 

>>> exec("x=6\ny=7\nprint(x+y)")

                

13

 

That seems to work. Again, if I copied and pasted the same as three lines,
it fails.

 

This works too:

 

>>> dothis = """x=6

y=7

print(x+y)

"""

                

>>> exec(dothis)

                

13

 

I did some HW before writing this and some suggest ipython has a better
repr.

 

Some solutions are arguably weird even if they work. One suggestion is to
wrap it all in an if that is always true and indent all subsequent lines:

 

if 1:

                x = 6

                y = 7

                print(x+y)

 

When I do a copy and paste of that including the otherwise meaningless if
statement, it works:

 

>>> if 1:

                x = 6

                y = 7

                print(x+y)

 

13

 

So, sure, I can stick nonsense in but wonder if there is a reasonable trick
like setting options to the interpreter or using an editor that feeds lines
slowly or .

 

But, yes, I can live with this or perhaps learn how to use a debugger than
might let me run things with breakpoints or .

 

 

 



More information about the Tutor mailing list