Python command line: ignore indentation

Question:

I am new to Python.

In short:

During scripting I continuously want to test small bits and pieces of my programs by copying/pasting some line(s) of code from my text editor to the command line Python interpreter. When these lines are indented (for example because they are part of a function), I’d like the interpreter to either ignore or not check indentation so that I don’t have to unindent them before copy/pasting. Is that possible?

In more details:

Here a simplified example of what I mean:

Let’s say my text editor contains the following module currently under development:

def MyFunc(arg):
    .../...
    if arg == 1:
        print "This is my function called with value 1."
        print "Done."
    else:
        print "This is my function called with value other than 1."
        print "Nothing else to say."
    .../...

And let’s say I simply want to test the 2 first print lines (lines 4 and 5 of the above code) straight away just to quickly check if at least that part of my module is behaving as expected. If I select both lines together I will at least select along the indentation for the second line (if not for both). When pasted at the command line, I’ll get an error for that indentation.

A simple enforced behaviour of the interpreter would be that it simply ignores indentation.

A more powerful behaviour would be to ask the interpreter to just not check the indentation. I.e. if indentation is there then the interpreter should try to use it so that I could still copy/past even a structured piece of code (e.g. lines 3 to 8 of the above code). But in case there are indentation errors it would just ignore them.

If there’s no way to do what I’m asking for here, then are there tricks for doing something similar: a simple way to quickly check pieces of your code without having to run the whole program every time you just want to tune small parts of it here and there.

NB 1: unindenting is NOT the solution to what I am looking for.

NB 2: having an interpreter together with a copy/paste capability offers a very powerful way of easily testing code but the explicit indentation mechanism of Python is a strong drawback to such a usage of the interpreter as described here if a turnaround cannot be found. Would be a pity.

Asked By: Joel Daroussin

||

Answers:

In such case, I use following trick (prepend if 1:):

>>> if 1:
...     print 1
...
1
Answered By: falsetru

The ipython %cpaste function will allow you to paste indented code and work properly:

In [5]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:        print "This is my function called with value 1."
:        print "Done."
:^D<EOF>
This is my function called with value 1.
Done.
Answered By: Wooble

Well you can create dummy code blocks , so that after copy-paste your code indents the correct way.
If the code from where you are copying looks like this:

...
    ...
        <python code to be copied>

Then in the shell, write:

>>> if True:
>>>     if True:
>>> <Paste your code here>
Answered By: Sudipta

Can you just avoid copying the tabs?

Notepad++ and Visual Studio allow rectangular selection by holding Shift + Alt when selecting text.

Whatever editor you use probably allows that as well.

Yes, this doesn’t answer your question as asked, but maybe it will solve your problem.

edit:

gvim allows rectangular selections that includes all line contents by Ctrl-l v Ctrl-q $ Shift-Down or Up arrow.
Thanks to Joel for clarifying this.

This link explains and provides other alternatives within vim:
http://vim.wikia.com/wiki/Add_trailing_blanks_to_lines_for_easy_visual_blocks

Answered By: dss539