Python program to find number of local variables in a function
Given a Python program, task is to find the number of local variables present in a function.
Examples:
Input : a = 1
b = 2.1
str = 'GeeksForGeeks'
Output : 3
We can use the co_nlocals()
function which returns the number of local variables used by the function to get the desired result.
Code #1:
def fun():
a = 1
str = 'GeeksForGeeks'
print (fun.__code__.co_nlocals)
|
Code #2:
def geek():
pass
def fun():
a, b, c = 1 , 2.25 , 333
str = 'GeeksForGeeks'
print (geek.__code__.co_nlocals)
print (fun.__code__.co_nlocals)
|
Last Updated :
10 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...