Python is considered as one of the languages with the simplest syntax, along with the huge applicability. There are some common mistakes that developers make when working with the language. Here are some of the most common mistakes that Python developers make.
1. Error handling
Errors in Python have a very specific form, called a traceback. When you forget a colon at the end of a line, accidentally add one space too many when indenting under an if statement, or forget a parenthesis, you will encounter a syntax error. This means that Python couldn’t figure out how to read your program.
To indicate a block of code in Python, each line of the block has to be indicated by the same amount.
In Python, unlike other languages, indentation means a lot more than making code look clean. It is required for indicating what block of code a statement belongs to. Many characteristics depend on indentation. Some indentation errors in Python are harder to spot than others. For example, mixing spaces and tabs can be difficult to spot in a code. If that happens, whatever is seen in the editor may not be the thing that is seen by the Python when the tabs are being counted as a number of spaces. Jupyter notebook automatically replaces tabs with spaces, but it gives an error in most cases. To avoid this mistake, for the entire block, all spaces or all tabs must be used.
3.Misusing The __init__ Method
The init is a reserved method in python classes used as constructors. In object-oriented terminology, it is called as a constructor And it gets called when Python allocates memory to a new class object. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class. The objective is to set the values of instance members for the class object. Trying to explicitly return a value from the init method implies that the user wants to deviate from its actual purpose.
4. Class variables use
In Python, class variables are internally handled as dictionaries and follow what is often referred to as Method Resolution Order or MRO. It defines the class search path used by Python to search for the right method to use in classes with multi-inheritance. This causes a Python problem unless it’s handled properly. Consider the following example:
print(" In class A")
print(" In class B")
r = B()
the methods that are invoked is from class B but not from class A, and this is due to Method Resolution Order(MRO). The order that follows in the above code is- class B - > class A. This causes a Python problem unless it’s handled properly.
Python has late binding behavior. There is often confusion among Python developers regarding how Python binds its variables. What it means is that it binds its variables in closures or in the surrounding global scope and hence the values of variables used in closures are looked up at the time the inner function is called.
6. Python Standard Library Module Names
Python is rich with library modules which comes out of the box. A common mistake is using the same name for a module and a module in the Python standard library. This will lead to importing another library which will then try to import the module in the Python standard library and since there will be a module with the same name, the other package will import the defined module, in place of the Python standard library module.
7. LEGB Rule
Python scope resolution is based on what is known as the LEGB rule, which is shorthand for Local, Enclosing, Global, Built-in. Python uses a different approach for scoping variables than other programming languages. If a user makes an assignment to a variable in a scope, that variable is automatically considered by Python to be local to that scope and shadows any similarly named variable in an outer scope. This error is particularly common for developers when using lists.
Register for our upcoming events:
- WEBINAR: HOW TO BEGIN A CAREER IN DATA SCIENCE | 24th Oct
- Machine Learning Developers Summit 2020: 22-23rd Jan, Bangalore | 30-31st Jan, Hyderabad
Enjoyed this story? Join our Telegram group. And be part of an engaging community.
Our annual ranking of Artificial Intelligence Programs in India for 2019 is out. Check here.
Provide your comments below
What's Your Reaction?
Found a way to Data Science and AI though her fascination for Technology. Likes to read, watch football and has an enourmous amount affection for Astrophysics.