Python 程式,找到函式中的區域性變數數
在本文中,我們將學習如何解決以下問題陳述。
問題陳述 - 給定一個函式,我們需要顯示該函式中的區域性變數數。
現在,讓我們觀察下面實現中的解決方案 -
示例
# checking locals
def scope():
a = 25.5
b = 5
str_ = 'Tutorialspoint'
# 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 程式來找到函式中的區域性變數數
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP