How to configure interactive python so that empty lines inside methods

Is it possible to test methods inside interactive python and store empty lines in them?

def f1(): import random import time time.sleep(random.randint(1, 4)) 

This gives a known error.

 IndentationError: unexpected indent 

So yes, the workaround is to delete all empty lines inside the functions. I would like to know if this is really necessary to work in interactive mode / REPL.

thanks

+7
source share
2 answers

There may not be much help, but it works if blank lines are indented. Points shown for clarity:

 def f1(): ....import random ....import time .... ....time.sleep(random.randint(1, 4)) 
+8
source

One option, especially for copying code into an interactive interpreter, is to embed it in a string literal and exec it:

 exec(r''' def f1(): import random import time time.sleep(random.randint(1, 4))''') 

If the code you entered already uses the strings ''' , surround it with quotation marks r"""...""" . If he already uses both ''' and """ , this will not work.

0
source

All Articles