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.
Without indentation, Python does not know which statement to execute next or which statement belongs to which block. This will lead to IndentationError .
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.
In such case, I use following trick (prepend if 1:
):
>>> if 1: ... print 1 ... 1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With