Pasting code into the cmdline interpreter

eryk sun eryksun at gmail.com
Thu Sep 22 03:42:40 EDT 2016


On Thu, Sep 22, 2016 at 5:12 AM, Veek M <vek.m1234 at gmail.com> wrote:
> 2. Blank lines in my code within the editor are perfectly acceptable for
> readability but they act as a block termination on cmd line.

You can write a simple paste() function. For example:

    import sys
    paste = lambda: exec(sys.stdin.read(), globals())

    >>> paste()
    class Foo:
        """test class"""

        def spam(self):
            '''test method'''
            return 'eggs'

    >>> Foo().spam()
    'eggs'

In a Unix terminal and IDLE, stdin.read() is terminated by typing
Ctrl+D at the start of an empty line. For the Windows console, it's
Ctrl+Z, Enter. Actually in a Unix terminal the cursor can also be at
the end of a line, but a bug in Python requires pressing Ctrl+D twice
in that case.



More information about the Python-list mailing list