Python’s LEGB Rule

Recently I was working on a piece of code when I had problems debugging it. The usual resort lies in going through all of stackoverflow questions to see if there is someone in some part of the world in the same state as you.

Eventually, I stumbled upon a question to which the response was so perfect I couldn’t help but write about it.

Here is that response.

The most amazing part about this response lies in the last paragraph. The LEGB rule.

As programmers, at times we tend to forget how the workflow of the code is. This LEGB rule is a gentle reminder of how simple things are.

Local -> Enclosing -> Global -> Built-in.

Happy coding.

Originally published at on October 8, 2015.