Some years ago I boarded into the chance of acquirements Python, I already knew some other programming languages like PHP (the first accent which alien me to web development), JavaScript (to which I was already pretty good at it, and was autograph a UI library) and C# which was amenable for my income at the time.

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

Misunderstanding Python scopes

Python scope resolution is based on what is known as the LEGB rule, which is autograph 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 archetype below:

webrok

For the code above I’d have accepted it to work, and altering the global variable x to assuredly printing 6. But, it can get weirder, let’s look at the afterward adapted code:

webrok

What in the world is going on? In one code snippet, the global variableXgives anUnboundLocalError. However, when we try to print the variable, it works. The has to do with scoping. When you make an appointment to a capricious in a scope (e.g. the action scope), that capricious becomes local to that scope and caliginosity any analogously named capricious in the outer scope. This is what happened in the first book when we didx = 1.

If what we are intending is to access the global variablexas in the case of our functionfoo(), we could do commodity like this:

webrok

By using the keywordglobal, it allows the inner scope to access the capricious declared in the global scope, acceptation variables that are not authentic in any function. Similarly, we could usenonlocalto aftermath a agnate effect:

webrok

nonlocalasglobalallows us to access variables from an outer scope, however, in the case ofnonlocal, you can bound an object on a parent scope or the global scope.

Modifying a list while bombastic over it

Though this error is not only common to Python, it’s an error commonly spotted among new Python developers, and even to some accomplished developers. Though sometimes it may not seem so obvious, under assertive occasions we end up modifying the array we’re currently bombastic consistent in unappropriated behavior. Or if we are lucky, we get an error and easily notice it.

But let me give you an archetype of what I mean. Let’ say that given an array you need to reduce that array to accommodate only the even elements, you may attack to do commodity like:

webrok

In the book described, when deleting an aspect for a list or array while iterating, we get an error as we try to access an item that is not there anymore. This is bad convenance and should be avoided, there are better ways to accomplish agnate things in Python, among them are list comprehensions:

webrok

You could also use thefilterfunction to accomplish the same thing. Though it works, some argue this is not the Pythonic way of doing it, and I kind of agree. But I don’t want to get into the middle of that discussion, I’d rather give you the options, and you can analysis and decide:

webrok

Variable bounden in closures

I’d like to start with a quiz I posted on cheep (@livecodestream) where I asked people about what they think the result of the afterward atom would be:

webrok

For many people, myself included, the first time we appointment this botheration we think that the result is:

webrok

However, the code absolutely resulted in commodity absolutely altered and I was very puzzled at this. What is absolutely accident is that Python would do a late-binding behavior, according to which the values of variables used in closures are looked up at the time the inner action is called. So in our example, whenever any of the alternate functions are called, the value of i is looked up in the surrounding scope at the time it is called.

A band-aid to this botheration may seem a bit ‘hacky,’ but it absolutely works:

webrok

By using the absence altercation of the lambda action to pass the value of i , we can accomplish the functions to do the adapted behavior. I was very puzzled by this solution, and I still accede it not very elegant, however, some people love it.

Name clashing with Python Accepted Library modules

This issue was absolutely pretty common when first started alive with Python. Even today, I sometimes make this mistake. The issue comes as a result of naming one of your modules with the same name as a module in the accepted library that ships with Python. For example, you might have a module named email.py in your code, which would be in battle with the accepted library module of the same name.

Perhaps the name clashing by itself won’t accomplish any issues with your code, but sometimes we override a action or module of the Python accepted library, which is later used in an installed library, and it conflicts either by throwing errors or misbehaving. In any case, it’s a bad bearings to have.

A archetypal aberration is the following:

webrok

By simply creating a capricious named list we broke the access to the list function. Even though there are other ways of accessing it (e.g. __builtins__.list()), we should avoid this kind of name.

This commodity does not cover all the common mistakes developers do when coding in Python, but rather those things I struggled the most. If you want to know more about how to write great Python code and alienated some other mistakes I acclaim you to read Make Your Code Great, Python Style.

Read next: Apple wins tax battle adjoin EU, avoids $15B fine

Pssst, hey you!