Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python IndentationError: too many levels of indentation

Tags:

python

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.


like image 743
AsTeR Avatar asked Dec 09 '11 00:12

AsTeR


People also ask

What is an indent in Python?

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!")

What causes unexpected indent error in Python?

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.

Should both print () statements use the same level of indentation?

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.

What is the purpose of indentationerrors?

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.


1 Answers

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. :)

like image 102
Noufal Ibrahim Avatar answered Oct 16 '22 23:10

Noufal Ibrahim