Devs

You’re probably making these Python mistakes — here’s how to avoid them – The Next Web

Some years ago I embarked into the adventure of learning Python, I already knew some other programming languages like PHP (the first language which introduced me to web development), JavaScript (to which I was already pretty good at it, and was writing a UI library) and C# which was responsible for my income at the time.

I learned Python by working on an app solo, and thus I incorporated many JavaScript and C# way of doing things into my code, which was terrible, though sometimes it worked. It took me some time, reading other’s people code and working with others to actually become better at the language. Today I’d like to go through with you on some of the mistakes I did (code-wise) when learning Python.

Misunderstanding Python scopes

Python scope resolution is based on what is known as the LEGB rule, which is shorthand for Local, Enclosing, Global, Built-in. Even though it looks pretty simple, it’s was very confusing for me at the time. So, let’s have a look at the example below:

For the code above I’d have expected it to work, and altering the global variable x to finally printing 6. But,
Source…