在本文中,我们将学习下面给出的问题陈述的解决方案。
问题陈述-给我们一个函数,我们需要显示该函数中局部变量的数量。
现在让我们在下面的实现中观察解决方案-
# checking locals
def scope():
a = 25.5
b = 5
str_ = 'Nhooo'
# main
print("Number of local varibales available:",
scope.__code__.co_nlocals)输出结果
Number of local varibales available: 3
# 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)输出结果
Number of local varibales available: 0 Number of local varibales available: 4
在本文中,我们了解了如何制作Python程序来查找函数中的多个局部变量。