Python program to find number of local variables in a function


In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a function, we need to display the number of local variables in the function.

Now let’s observe the solution in the implementation below −

Example

 Live Demo

# checking locals
def scope():
   a = 25.5
   b = 5
   str_ = 'Tutorialspoint'
# main
print("Number of local varibales available:",
scope.__code__.co_nlocals)

Output

Number of local varibales available: 3

Example

 Live Demo

# checking locals
def empty():
   pass
def scope():
   a, b, c = 9, 23.4, True
   str = 'Tutiorialspoint'
# main
print("Number of local varibales
available:",empty.__code__.co_nlocals)
print("Number of local varibales
available:",scope.__code__.co_nlocals)

Output

Number of local varibales available: 0
Number of local varibales available: 4

Conclusion

In this article, we have learned about how we can make a Python program to find a number of local variables in a function

Updated on: 20-Dec-2019

716 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements