一.locals函式語法
python 內置函式locals比較簡單,直接以字典的形式回傳當前位置的所有區域變數,語法如下:
locals()
回傳值:回傳一個字典,該字典包含當前位置的所有區域變數;
二.locals函式使用
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個人博客地址): shuopython.com @WeChat Official Account(微信公眾號):猿說python @Github:www.github.com @File:python_locals.py @Time:2020/3/2 10:25 @Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累! """ def func2(arg): # 兩個區域變數:arg、z z = 1 print(locals()) def func1(): # 兩個區域變數:x、y x = "shuopython.com" y = 2 print(locals()) if __name__ == "__main__": func1() func2("python教程-猿說python")
輸出結果:
{'y': 2, 'x': 'shuopython.com'}
{'z': 1, 'arg': 'python教程-猿說python'}
猜你喜歡:
1.python str/bytes/unicode區別詳解
2.python bytearray/bytes/string區別
3.python bytes和string相互轉換
轉載請注明:猿說Python ? python locals函式
技術交流、商務合作請直接聯系博主 掃碼或搜索:猿說python
猿說python
微信公眾號 掃一掃關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/180072.html
標籤:Python
下一篇:Python學習筆記(五)序列
