I've a part of my python program which is generated, the generated codes contains a lot of nested if
/ else
, my problem is that a lot can be too much and I got this Error when running my code:
IndentationError: too many levels of indentation
I've read that this was some limitation defined on the low level of the python interpreter, does anybody know how I can find a workaround for it? Some interpreter parameters would be fine, the only solution proposal I've found suggests to recompile Python in order to set a different value for the MAXINDENT
constant, which is not exactly what I'm dreaming of.
EDIT : The code is a big bunch of nested if...else
, it's dirty but it was the quickest I found to port a complex decision tree to Python. I know how dirty it is; I did not write it myself — I did not even plan to edit it (I would rather touch the generator).
I know I can modelize this decision tree in other fashions. What I would like is a way simpler than that, like tweaking the interpretor if possible.
EDIT 2 : Now I've done some refactoring, and my tree is stored as a dictionary: The loading of the file gives a new error :
s_push: parser stack overflow
MemoryError
Here again I found a resource suggesting some tweaks of the interpreter's headers.
An indent is a specific number of spaces or tabs denoting that a line of code is part of a particular code block. Consider the following program: def hello_world (): print ( "Hello, world!")
Python does not have this feature, so the language depends heavily on indentation. The cause of the “IndentationError: unexpected indent” error is indenting your code too far, or using too many tabs and spaces to indent a line of code. 81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp.
Both print () statements should use the same level of indentation because they are part of the same if statement. We’ve made this revision above. Our code successfully prints out all the purchases worth more than $25.00 to the console. “IndentationError: unexpected indent” is raised when you indent a line of code too many times.
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. IndentationErrors serve two purposes: they help make your code more readable and ensure the Python interpreter correctly understands your code.
Your generator is producing bad code. You should treat this exactly as you would if it were producing syntactically invalid code.
Use functions, dictionary dispatching and any other thing that might occur to you to reduce the depth.
OTOH, thanks for showing me that Python does really have a maximum depth. I didn't know that. :)
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