Tuesday 11 August 2020, by Tobias Foster
Many developers start building their programming skills by learning Python. The reason why this language is so popular is due to its incredibly simple syntax and notable applicability. Even though it’s easy to learn, that doesn’t mean you won’t make any mistakes while writing code. If you have any type of programming experience, you know that mistakes are bound to happen regardless of how careful you are. Here are some of the main pitfalls of the Python programming language.
Name Clashing
There is a wealth of Python standard library models that comes out of the box. While this is definitely a useful feature, it can cause some problems when you’re writing code in this programming language. If you’re not careful, you’ll often find yourself running into a name clash between your module and one of the standard library modules. That means that you have to consciously avoid name clashing.
In case one of your modules and a module in the standard library shares the same name, it could lead to some problems. For example, you may import a library that then tries to import a module in the standard library. However, since a module with the same name already exists, the other package can import the version you created instead of the one in the standard library. As you can probably guess, this can lead to some pretty bad errors.
Using Globals
Global variables represent variables that are created outside of a function or in global scope. In other words, this type of variable can be accessed inside or outside of the function. In case you wanted to create a variable inside of a function, it would be local. That means that it could be used only inside the function. If you use global keywords in Python, you’re able to make changes to global variables in a function locally. This makes it appealing to beginners who believe that using globals saves them a lot of time.
What a lot of Python beginners believe is that global variables allow you to pass all the arguments required for the function. Nevertheless, this is simply a common misconception. What global actually do is hide the actions. It’s worth noting that you using global variables in functions can lead to errors that you might have difficulty solving. You’re bound to experience a lot of confusion during debugging in case you modify globals in a local function. Instead what you should do is pass the variable in as an argument. Afterwards, modify the variable and have it returned at the end of the function.
Copy
Many Python beginners don’t seem to understand the concept of copy. In case you’re new to programming, you most likely think that if you decide to copy an object and make changes to the new version, then those changes won’t have any effect on the original object. However, that’s exactly what will happen if you choose to copy an object using assignment statement. The assignment statement feature will create a duplicate object, but it’ll also bind them.
That’s why you should take a different approach to copying. You basically have two options when it comes to this operation in Python – deep copy and shallow copy. In some cases, you might not be able to stop the differences between using a shallow and deep copy. This is true in case you want to copy variables that can fit into a single byte of data, such as string, floats, and integers. But in case you plan on working with dictionaries and lists, it’s a good idea to exclusively use deep copying.
What a deep copy does is create a new compound object and then recursively inserts copies of nested objects in the original elements. That means that the deep copy option will copy everything from the original object without any bindings. On the other hand, a shallow copy creates a new compound object, after which it inserts references to elements located in the original. In other words, if you use shallow copy to create a duplicate object, then any changes you make on elements within nested objects in the copy will also appear in the original.
Mutable Defaults
Not having a clear understanding of mutable defaults is one of the most common pitfalls you can face while writing code in Python. The reason why this is such a common issue is because mutable defaults are incredibly unique objects in this programming language. In case you’re not familiar with mutable objects, they represent objects that can change either their contents or states during runtime. Of course, immutable objects like string, bool, and float can’t do this.
Some of the most common examples of mutable data types include dictionaries, sets, and lists. In case default arguments in functions are mutable, you would expect them to change each time the function gets called. Nevertheless, this doesn’t happen because when you use default arguments in Python, they are evaluated just once. This occurs only at the time the function is defined. That means that your mutated default argument would remain the same every time the function is called. What you can do to avoid this problem is simply set the value of your argument to nothing. Make sure to also include a conditional that will modify the list in case the element doesn’t exist
Not Using the __init__ Method Properly
The __init__ method represents a reserved method in Python classes and is something similar to constructors in languages like C++ and Java. In case you’re familiar with constructors, then you know that these methods are used to initialize the state of an object. This method gets called automatically when Python allocated memory to a new class object.
Although the purpose of this method is setting up instance values to the new class object, many developers use it the wrong way. Namely, they try to explicitly return a value from the __init__ method. This is a bad practice because that’s not what the purpose of the method is. Instead, what you can do to explicitly return a value is either define a new instance method or simply use a different reserved method.
Not Using Iterators
Even if you have experience with other programming languages, there’s still a good change that you won’t be using iterators when writing code in Python. And this can be a huge mistake. In case you have basic programming knowledge, you probably know how to create an iterator for integers that you can use for objects like lists. However, what you should also know is that the majority of built-in containers in Python, such as tuples and lists are iterables. That means that you can get an iterator from the object. You can use the iter() function to get an iterator from these objects. Keep in mind that you can traverse multiple lists at the same time by using the zip function in a for-loop.
Incorrect Indentation
If you’ve used proper indentation in other programming languages, you most likely did it simply to make your code look clean. What makes Python different from other languages is that indentation serves a much bigger purpose here. It is used to indicate which block of code a certain statement belongs to. In case you skip the indentation, Python will give you an error. You can choose the number of spaces you want to use to indicate a block of code. But if you don’t use this number in the same block of code, you’ll get an error.
Conclusion
Often times, you might not even realise you’re making mistakes while coding. That’s why it’s important to understand the main pitfalls of a programming language before you write code. Otherwise, you may lose a lot of time wondering what you did wrong. If you’re familiar with the important nuances of this Python, you’ll be more efficient at programming.