4 Coding Mistakes I Did When I was Learning Python

Learn together the Python concepts I struggled the most with 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 the responsible for my income at the time. I learned Python by working on an app on my own, 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. #1 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 looks very simple, it was a bit confusing for me at the time, for example, consider the following example: x = 5 def foo(): x = 1 print(x) foo() ———– Output ———– Traceback (most recent call last): File “”, line 1, in File “”, line 2, in foo UnboundLocalError: local variable ‘x’ referenced before assignment For the code above I’d have expected it to work, and altering the global variable x to finally printing 6. But, it can get weirder, let’s look at the following altered code: y = 5 def foo_y(): print(y) foo_y() ———– Output ———– 5 What in the world is going on? In one code snippet, the global variable X gives an UnboundLocalError however, when we just try to print the variable it works. The reason has to do with scoping. When you make an assignment to a variable in a scope (e.g. the function scope), that variable becomes local to that scope and shadows any similarly named variable in the outer scope. This is what happened in the first scenario when we did x = 1. If what we are intending is to access the global variable x as in the case of our function foo() we could do something like: x = 5 def foo(): global x x = 1 print(x) foo() ———– Output ———– 6 By using the keyword global allows the inner scope to access the variable declared in the global scope, meaning variables that are not defined in any function. Similarly, we could use nonlocal to produce a similar effect: def foo(): x = 5 def bar(): nonlocal x x = 1 print(x) bar() foo() ———– Output ———– 6 nonlocal as global allows you to access variables from an outer scope, however, in the case of nonlocal, you can bound to an object on a parent scope or the global scope. #2 Modifying a list while iterating over it Though this error is not only common to Python, it’s an error that I’d find out it’s pretty common among new Python developers, and even to some experienced developers as well. Though sometimes may not seem so obvious, under certain occasions we end up modifying the array we are currently iterating resulting in unappropriated behavior, or if we are lucky we get an error and easily notice it. But let me give you an example of what I mean, let’ say that given an array you need to reduce that array to contain only the even elements,  » Read More

Like to keep reading?

This article first appeared on livecodestream.dev. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply