Home / Technology / You are probably making these Python mistakes – here’s how you can avoid them

You are probably making these Python mistakes – here’s how you can avoid them

A few years ago I started the adventure of learning python. I already knew some other programming languages ​​like PHP (the first language that introduced me to web development), JavaScript (in which I was already pretty good at it and wrote a user interface) library) and C #, which was responsible for my income at the time were.

I learned Python solo by working on an app, so I integrated many JavaScript and C # methods into my code, which was terrible, although it sometimes worked. It took me some time to read other people’s code and work with others to actually master the language better. Today I want to discuss with you some of the mistakes I made (in terms of code) when learning Python.

Misunderstandings in Python realms

The resolution of the Python area is based on the so-called LEGB Rule, which is short for L.ocal, E.nclosing, Gglobal, B.Uilt-in. Although it looks pretty simple, it was very confusing for me at the time. So let’s look at the following example:

For the code above, I would have expected it to work and change the global variable x to finally print 6. But it can get weirder. Let’s look at the following changed code:

What on earth is going on? The global variable in a code snippetXthere isUnboundLocalError. However, if we try to print the variable, it works. It has to do with scoping. If you assign a variable in an area (e.g. the functional area), this variable becomes local for this area and shades all similarly named variables in the outer area. This is what happened in the first scenario when we did itx += 1.

If we intend to access the global variablexas in the case of our functionfoo()we could do something like this:

With the keywordglobalenables the inner area to access the variable declared in the global area, ie variables that are not defined in any function. We could also usenonlocalto achieve a similar effect:

nonlocalhowglobalhowever, allows us to access variables from an outside area, in the case ofnonlocalyou can bind an object to a higher-level area or the global area.

Modify a list as it goes through

Although not only common with Python, this bug also occurs frequently with new Python developers and even with some experienced developers. Although it may not seem so obvious at times, in certain circumstances we change the array that we are iterating, resulting in inappropriate behavior. Or if we are lucky we will get an error and notice it easily.

But let me give you an example of what I mean. For example, suppose you have to collapse this array so that it contains only the even elements. You may try the following:

In the scenario described, when deleting an item for a list or an array during the iteration, an error message is displayed when we try to access an item that no longer exists. This is bad practice and should be avoided. There are better ways to do similar things in Python. This includes list comprehension:

You can also use thatfilterFunction to do the same. Although it works, some argue that this is not the pythonic method, and I agree with it. But I don’t want to be in the middle of this discussion, I’d rather give you the options, and you can research and decide:

Variable binding in closures

I want to start with a quiz I posted on Twitter (@livecodestream) where I asked people what they thought would be the result of the following snippet:

For many people, including me, the result is the first time we encounter this problem:

However, the code actually led to something completely different and I was very confused about it. What actually happens is that Python performs a late binding behavior, after which the values ​​of the variables used in closures are looked up at the time the inner function is called. In our example, when one of the returned functions is called, the value is of i is looked up in the surrounding area at the time of the call.

A solution to this problem may seem a bit “hacky”, but it actually works:

Use the Lambda function’s default argument to pass the value of i , We can generate the functions to achieve the desired behavior. I was very confused about this solution and still don’t find it very elegant, but some people love it.

Name conflict with Python Standard Library modules

This problem was actually quite common when I started using Python. I still make this mistake sometimes. The problem arises from naming one of your modules with the same name as a module in the standard library that comes with Python. For example, your code could contain a module called email.py, which contradicts the standard library module of the same name.

Perhaps the name that crashes by itself doesn’t cause problems with your code, but sometimes we overwrite a function or module of the standard Python library that will later be used in an installed library, and conflicts arise by either throwing errors become or behave badly. In any case, it is a bad situation.

A classic mistake is as follows:

By simply creating a variable with the name list We have access to list Function. Even if there are other ways to access it (e.g. __builtins__.list()) we should avoid this type of name.

This article doesn’t cover all the common mistakes developers make when coding in Python, but the things I’ve struggled with the most. If you want to learn more about how to write great Python code and avoid other mistakes, we recommend reading Make Your Code Great, Python Style.

This article was originally published on Live code stream by Juan Cruz Martinez, Founder and publisher of Live Code Stream. He is a software engineer with more than 10 years of experience in this area and works on a variety of projects, from open source solutions to enterprise applications. Happily married, with a child, officially engaged with JavaScript, in a love affair with Python and in the dream of the writer! You can read this original piece Here.

Live code stream is also available as a free weekly newsletter. Sign up for updates on all topics related to programming, AI and IT in general.

Continue reading:

Apple wins tax war against EU and avoids US $ 15 billion fine

Phew, hey you!

Would you like to receive the funniest daily tech newsletter in your inbox for FREE every day? Of course: Sign up for Big Spam here.

Source link