Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command line: ignore indentation

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.

like image 998
Joel Daroussin Avatar asked Jul 12 '13 13:07

Joel Daroussin


People also ask

What happens if we skip indentation in Python?

Without indentation, Python does not know which statement to execute next or which statement belongs to which block. This will lead to IndentationError .

How do I fix the TabError in Python?

The Python “TabError: inconsistent use of tabs and spaces in indentation” error is raised when you try to indent code using both spaces and tabs. You fix this error by sticking to either spaces or tabs in a program and replacing any tabs or spaces that do not use your preferred method of indentation.


2 Answers

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

>>> if 1: ...     print 1 ... 1 
like image 80
falsetru Avatar answered Sep 20 '22 08:09

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. 
like image 37
Wooble Avatar answered Sep 18 '22 08:09

Wooble