write a python program to detect the number of local variables declared in a function

In this article, we will learn about the solution and the number of local variables present in a function.

Detect the number of local variables declared in a function

Example:-

Input : a = 1
b = 2.1
str = ‘local Variables in Function’

Output : 3

Code 1): python program to detect the number of local variables declared in a function

write a python program to detect the number of local variables declared in a function
python program to detect the number of local variables declared in a function

# Code 1):-

def scope():

   a = 45

   b = 87

   str_ = ‘The local or global’

# main

print(“Number of local varibales available:”,scope.__code__.co_nlocals)

 OUTPUT:- Number of local varibales available: 3

How to write the program to detect the number of local variables declared in the function

  1. first, we take a function with the name of the scope
  2. after we take some variables to check the local or global in the functions
  3. Use (.__code__.co_nlocals) built-in function to detect the number of locals variables in the function
  4. The answer is 3

code 2):-another method to detect the number of local variables declared in python

write a python program to detect the number of local variables declared in a function
write a python program to detect the number of local variables declared in a function

def scope():

   a,b,c = 45,455,”hello python”

# main

print(“Number of local varibales available:”,scope.__code__.co_nlocals)

OUTPUT:- Number of local variables available: 3

In this program, we use the same method but we take variables in the same line or in serial

Out is same 3.

Conclusion

In this article, we have learned how to write a python program to detect the number of local variables declared in a function in a simple way using the built-in functions

Leave a Comment