The 10 Most Atrocious Python Mistakes Aspirants Often Make!
Python programming language is a simple, sophisticated, and straightforward algorithm that can perplex Python developers – mainly newbies. However, at some point in any Data Scientist or programming aspirant’s career, it is probable that the aspirant has to choose a new language and enhance their skills.
Especially, Python is an object-oriented, interpreted programming language. Its advanced concept in data structures, blended with dynamic binding and dynamic typing, makes it a most fascinating language to avail as a glue or scripting language to bridge existing services or components and for Rapid application development.
Aspirants often commit certain mistakes which are quite common while using this high-level programming language. In this article, let’s discuss the 10 most atrocious Python mistakes aspirants often make.
The Top 10 Awful Python Mistakes Newbies Make
1. Defining Main Function in Python
This is the most common mistake made by coding aspirants. As mentioned in the introduction part since Python programming is a high-level scripting language, one can define functions that could be called in REPL mode.
Example:
def super_fun()
print (“Good morning”)
super_fun()
With the help of the above example, we can execute the super_fun function when it’s called from the CLI python no_main_func.py. However, if the programmer wants to use the code again as a module in a book, what would be the case?
No main func
import no_main_func
Good morning
The super_fun is now executed accordingly when the script is imported. This is a simple example, but if you are executing some heavy computational operations, or giving rise to an activity that creates multiple threads. In this case, Aspirants want to avoid running the code automatically on import, so this is how one can ward off the same.
def super_fun():
print(“Good morning”)
if_name_==”_main_”:
# execute only if run as a script
super_fun()
As you can see, unexpected behaviors have been prevented when you import it as a module.
2. The boolean mess
Like in every other programming language, the doubt of what’s supposed to be contemplated as a Boolean “true” value is a most common confusion in Python too.
Let us understand with the help of an example:
>>> 0 == False
True
>>> 0.0 == False
True
>>> [] == False
False
>>> {} == False
False
>>> set() == False
False
>>> bool(None) == False
True
>>> None == False
False
>>> None == True
False
In the above example, the zero value for any numeric data type is appraised to be false. However, empty collections such as sets, lists, or dictionaries are true. This can be tedious as a variable can be kept undefined and later incorporated in a contrast that will deliver an unpredicted outcome.
3. Misusing Python Scope Rules
Python scope resolution is built on the LEGB rule, that is, contemplated as an abbreviation of Local, Enclosing, Global, and Built-in. Seems easy-peasy, isn’t it? Actually, there are certain subtleties to the way it runs in Python programming language that delivers coders the common error and this is one of the most atrocious python mistakes that freshers do.
4. Misunderstanding Function Arguments by Reference And Value
The high-level programming language Python has a weird way of incorporating arguments in functions and methods. Aspiring coders who are shifting to Python from other languages like C++ or Java may misunderstand the way that the interpreter runs with arguments.
5. Class Variables Confusions
Object-oriented programming intends to put together problems in a way that reflects the real world, however, it can become tedious to newbies in the coding space.
6. Updating a list while iterating over it
Removing a thing or any item from an array or list while iterating over it could be problematic, which is familiar to the majority of experienced software developers. However, most of the time it becomes cumbersome for aspirants. Sometimes professional Python developers could face problems by this in code that is more complicated.
7. Ineffectiveness in understanding the differences between Python 2 & Python 3
Freshers fail to understand the actual difference between Python 2 & Python 3 since the two versions are quite similar. In Python 3, the exception object could not be accessed beyond the scope of the except block. The primary reason is that it would grasp a reference cycle with the stack frame in memory till the garbage collector continues to operate.
8. Float Data Types
This is one more addition to the 10 most atrocious Python mistakes. Beginners misunderstand float as a simple type. For instance, the below-mentioned example highlights the difference between defining the identifier of float types and simple types.
>>> a = 10
>>> b = 10
>>>
>>> id(a) == id(b)
True
>>> c = 10.0
>>> d = 10.0
>>>
>>> id(c) == id(d)
False
>>> print(id(a), id(b))
9788896 9788896
>>> print(id(c), id(d))
140538411157584 140538410559728
Find another example below, an easy arithmetic operation that is simple to solve, but you will get the unpredicted results because of the comparison operator.
>>> a = (0.3 * 3) + 0.1
>>> b = 1.0
>>> a == b
False
Programmers can avoid the same with the help of the following function mentioned in the example below:
def super_fun(a:float, b:float):
return True if abs(a-b) < 1e-9 else False
if __name__ == “__main__”:
# execute only if run as a script
print(super_fun((0.3*3 + 0.1),1.0))
9. Perplexity over how Python binds variables in closures
Beginners often get confused over this because Python’s late binding attribute states that the values of variables exploited in closures could be looked up at the time while calling the inner function.
10. Name collision with Python Standard Library Modules
One of the interesting things about this high-level language is the opulence of library modules. If beginners are not avoiding it smartly, names get clash between one of your modules and the one which already exists in the library.