Like any other form of coding, Python works with code. Developers and testers alike can learn to work with it if they have the knowledge of coding under their belts. However, everyone, even expert testers can easily to run into mistakes. But not to worry!
In this quick guide, I’ll introduce you to 8 of the most common mistakes in Python, and how to avoid them at all costs. Let’s dive right in! 🐍
1️⃣ Not Following The Rules
“Python goes by a set of rules called LEGB,” says Rebecca Hetherington, a tech writer. “LEGB stands for ‘Local,’ ‘Enclosing,’ ‘Global,’ and ‘Built-In’. This refers to the idea that when you assign to a variable in a scope, that variable is automatically considered local to that scope.
As a result, the variable shadows any similarly-named variable in an outer scope. Therefore, it’s important to follow these LEGB rules, so that you don’t end up with an ‘UnboundLocalError’”.
2️⃣ Misusing Expressions
In Python, you can specify that a function argument is optional by giving it a default value. But sometimes, this action is confusing when you mute the default value.
To avoid misusing expressions, supply a suitable value for the optional argument because the default value for a function argument is only looked at once- once the function is defined.
3️⃣ Modifying And Iterating A List
Too often, software developers make the common mistake of deleting an item from a list while iterating over it. But the good news is, Python uses special programming paradigms to combat that.
When used properly, paradigms can simplify and then streamline the code, so that no important item is deleted and iterated by accident.
4️⃣ Name Clashing
With many library modules under its belt, it’s not surprising that Python has much to offer for software developers. However, should you choose to avoid such wealth, you’ll risk name clashing. Clashing is when you confuse the name of one of your modules with a module with the same name in Python’s standard library.
This can result in you importing another library, which will mess with your standard library version of a module. So, avoid using the same names as those in the Python standard library modules. You can always request a name change by filing a Python Enhancement Proposal (PEP).
5️⃣ Incorrectly Using Class Variables
Class variables in Python are used as dictionaries and are referred to as Method Resolution Order (MRO). However, if one attribute isn’t found in a class, then that class lacks a property. That means that if you change what’s in a class, then other classes shouldn’t change along with it.
6️⃣ Not Specifying Parameters Correctly
“The except statement doesn’t take into account the list of exceptions, as one might suspect,” says Rory Barak, a tech blogger. “Instead, the syntax exception binds the exceptions to the optional second parameter specified. Afterward, the ‘IndexError’ exception is not detected being by the except statement, and not being specified as a parameter correctly”.
“One solution to this is to specify the first parameter as a tuple containing all needed exceptions while using ‘the’ as your keyword.”
7️⃣ Misusing “_ _ del _ _”
The “_ _ del _ _” method can only be used in specific scenarios, and not all the time. Otherwise, you’ll get an “AttributeError” exception. When “_ _del_ _” is invoked, a name in the code is set to “None”. Instead, use “atexit.register(),” which kicks off registered handlers before you shut down the interpreter.
8️⃣ Circular Module Dependencies
Finally, having circular module dependencies might seem harmless at first, since Python knows not to re-import something. However, you might run into an AttributeError exception at some point, which isn’t good. The solution? Modify a module with a ().
So there you have it! By avoiding these 8 common mistakes in Python, your code will not only make sense but will also work in the long run. Remember to always check your coding, and fix anything that seems a bit off. The last thing you want is to end up with code that’s chock-full of errors.
Also, remember that everyone – even experts – makes mistakes when it comes to Python and coding. So, when you make a mistake, fix it and try again. Don’t ever kick yourself for making a mistake 💪