Open In App

Gotchas in Python

Last Updated : 02 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a go-to language for most of the newcomers into the programming world. This is because it is fairly simple, highly in-demand, and ultimately powerful. But there are some cases which might confuse or rather trick a rookie coder. These are called “Gotchas”! Originating from the informal term “Got You!”, a gotcha is a case or scenario when the program plays the trick and results in an output that is quite different from what was expected. It should be noted that a gotcha is not an error or an exception. It is a perfectly valid code that results in an incorrect output only because we missed a teeny-tiny fact or point while writing our program. Therefore, we can also consider gotchas as “commonly made mistakes while coding”.

python-gotchas

Let’s take a look at some most common gotchas in Python3 and how to tackle them:

  1. The parenthesis gotchas :

    There are a few gotchas that arise when the parenthesis is used incorrectly or unnecessarily.

    Example:

    Python3




    # results in False
    print(5>2 == True

    
    

    Output:

    False

    This results in False because the above expression effectively means that 5>2 and 2==True. This implies, True and False. Hence, the final result is False.
    It can be corrected by the use of parenthesis.

    Python3




    # results in True
    print((5>2) == True)

    
    

    Output:

    True
    

    Here is one more example:

    Python3




    # results in False
    print(5 is (not None)) 

    
    

    Output:

    False

    This is because "is not" is different from "is" and "not" being used separately. The part (not None) equals True and 5 is True results in False. It can be corrected by not using any parenthesis.

    Python3




    # results in True
    print(5 is not None)

    
    

    Output:

    True
    

  2. “is”, “==”, “is not”, “!=” : This is a short but very common gotcha. Many new programmers often think that is and == are the same thing. But it’s definitely not!

    Python3




    # results in True
    print(1 == True)
      
    # results in False
    print(1 is True

    
    

    Output:

    True
    False
    

    On the other hand, is not and != are same.

    Python3




    # results in True
    print(1 != False
      
    # results in True
    print(1 is not False)

    
    

    Output:

    True
    True
    

  3. Default arguments gotcha :

    In Python, default arguments are declared only once when the function is run for the first time and from then on, the declared argument is used every time.

    Python3




    def appendNew(appendTo =[]):      
        appendTo.append(1)   
        return appendTo
          
    # Driver's code
    print(appendNew())
    print(appendNew())

    
    

    It’s expected that every time we call appendNew(), a new list will be created which will have 1 in it. But what happens is this:

    [1]
    [1, 1]
    

    The variable appendTo isn’t created again when the function is run for the second time. Instead, it’s created only the first time and used again and again. We can tackle it by:

    Python3




    def appendNew(appendTo = None):  
        if appendTo == None:
            appendTo =[]    
        appendTo.append(1)   
        return appendTo
          
    # Driver's code
    print(appendNew())
    print(appendNew())

    
    

    Output:

    [1]
    [1]
    

  4. Scope gotchas :

    Sometimes, we must keep in mind the scope of the variable we are dealing with, i.e whether it is a global scope(works but inside and outside of a function) or a local scope(works just inside the function).

    Example:

    Python3




    list1 = [1, 2, 3]
    def baz1():
      
        # the code works fine
        list1.append(4
        return list1
    def baz2():
      
        # Doesn't work fine
        list1 += [5]      
        return list1
          
    # Driver's code
    print(baz1())
    print(baz2())

    
    

    Output:

    [1, 2, 3, 4]
    
    Traceback (most recent call last):
      File "/home/ba0dfa25407638b061076b45ce165ce5.py", line 15, in 
        print(baz2())
      File "/home/ba0dfa25407638b061076b45ce165ce5.py", line 10, in baz2
        list1 += [5]      
    UnboundLocalError: local variable 'list1' referenced before assignment
    

    This happens because

    list1 += [5]

    means that we are assigning to the variable list1 but list1 is defined outside the scope of our function. While in baz1(), we are appending to list1 instead of assigning and hence it works fine.

  5. Variables are bound late in closures :

    Python has an infamous late binding behavior. By this, we mean that the value of a variable which is being iterated over is finalized to the value when it reaches its last iteration. Let’s look at an example:

    Python3




    def create_multipliers():
      
        # lambda function creates an iterable
        # list anonymously
        return [lambda c : i * c for i in range(6)] 
      
      
    for multiplier in create_multipliers():
        print multiplier(3)

    
    

    The expected result is of course:

    0
    3
    6
    9
    12
    15
    

    But what we get is:

    15
    15
    15
    15
    15
    15
    

    This is because when the loop iteration is complete, i has a value of 5 and hence 3*5 each time results in 15.
    It can be solved by:

    Python3




    def create_multipliers():
        return [lambda x, i = i : i * x for i in range(6)]
          
          
    for multiplier in create_multipliers():
        print(multiplier(3))

    
    

    Output:

    0
    3
    6
    9
    12
    15
    

  6. Mutating a list while iterating over it :

    This is the most common gotcha which new coders face almost all the time. While working with a list or other mutable items, if we mutate it while iterating over it, it’s certain to cause errors. It’s recommended that we create the copy of the list instead and mutate it rather than the original list.

    Python3




    # buggy program to print a list 
    # of odd numbers from 1 to 10
      
      
    even = lambda x : bool(x % 2)
    numbers = [n for n in range(10)]
      
    for i in range(len(numbers)):
        if not even(numbers[i]):
            del numbers[i]

    
    

    Output:

    Traceback (most recent call last):
      File "/home/92eed8bfd8c92fca3cf85f22e8cfd9ea.py", line 9, in 
        if not even(numbers[i]):
    IndexError: list index out of range
    

    But if we use a copy of numbers instead:

    Python3




    # working program to print a 
    # list of odd numbers from 1 to 10
      
      
    even = lambda x : bool(x % 2)
    numbers = [n for n in range(10)]
      
    numbers[:] = [n for n in numbers if even(n)]
    print(numbers)  

    
    

    Output:

    [1, 3, 5, 7, 9]
    


Similar Reads

Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
JavaScript vs Python : Can Python Overtop JavaScript by 2020?
This is the Clash of the Titans!! And no...I am not talking about the Hollywood movie (don’t bother watching it...it's horrible!). I am talking about JavaScript and Python, two of the most popular programming languages in existence today. JavaScript is currently the most commonly used programming language (and has been for quite some time!) but now
5 min read
Python | Visualizing O(n) using Python
Introduction Algorithm complexity can be a difficult concept to grasp, even presented with compelling mathematical arguments. This article presents a tiny Python program that shows the relative complexity of several typical functions. It can be easily adapted to other functions. Complexity. Why it matters? Computational complexity is a venerable su
3 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
Python | Convert list to Python array
Sometimes while working in Python we can have a problem in which we need to restrict the data elements to just one type. A list can be heterogeneous, can have data of multiple data types and it is sometimes undesirable. There is a need to convert this to a data structure that restricts the type of data. Convert List to Array PythonBelow are the met
3 min read
Article Tags :
Practice Tags :