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 程式來找到函式中的區域性變數數

更新時間: 20-12-2019

807 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.