Useful Python Functions

Introduction

This is the first part of the useful Python function series; the article explains four extremely useful Python functions

  • breakpoint( )
  • globals( )
  • locals( )
  • input( )

Let’s explore,

# breakpoint( )

As the name of the function suggests, the ‘breakpoint’ function is used for debugging the program instead of using the standard print statements. The ‘breakpoint’ function provides interactive debugging which makes bug detection easier. In the block of code where the debugging is required, you can insert a breakpoint using the ‘breakpoint’ function and you can print the variables for debugging. The breakpoint function only works with Python version 3.7 or above.

Let’s understand by a simple program for reading the file and let’s put a breakpoint after every line to print the variable and see what’s going on with the program, the names.txt file has 3 names in each line John, Mike, Sam

with open("names.txt") as file_in:
    lines = []
    stripNewLine = ''
    for line in file_in:
        stripNewLine = line.strip()
        breakpoint()
        lines.append(stripNewLine)

The program is being executed in Jupyter Notebook, for every line the breakpoint will be executed, and it opens an interactive debugging session with user

In the text field, a user can type a ‘stripNewLine’ variable to see the current state of ‘stripNewLine’ variable

To go to the next line, we need to enter ‘n’

The next name will be printed and to terminate the debugging session ‘exit’ needs to be entered. Use of this function can help a lot in the complex program, but the suggestion would be to use in actual block to quickly detect the issue.

#globals( )

The globals function returns the dictionary of the current global symbol table. The symbol table is maintained by the Python compiled and it contains the information about the program like method names, variables names, etc. On using globals() function it returns the dictionary of the global symbol table which is all global variables / other symbols in the program.

It also returns the program details as well, let’s declare a variable say 'name' and execute a global() function

name = "Mike"
globals()

The variable ‘name’ is shown along with the initialized value.

# locals()

Just like globals, locals returns the dictionary of the current local symbol table

def get_name():
    name = "Mike"
    print(locals())
    return name

get_name()

# input( )

The input() function is used for accepting inputs from the user through the command line and returns a String value. It’s not a rare function though

email = input("Enter your email-id:")
print(email)

age = input("Enter age:")
print(type(age))

The expectation is the age variable should have an int type, but input() by default returns string value, the explicit casting of variables is required in such cases

Summary

The article covered 4 useful Python functions, breakpoint, globals, locals, and input. In the upcoming articles, more such functions will be covered.


Recommended Free Ebook
Similar Articles