国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python基于locals返回作用域字典

瀏覽:63日期:2022-07-08 08:36:54

英文文檔:

locals()

Update and return a dictionary representing the current local symbol table. Free variables are returned by locals()when it is called in function blocks, but not in class blocks.

返回當前作用域內的局部變量和其值組成的字典

說明:

1. 函數功能返回當前作用域內的局部變量和其值組成的字典,與globals函數類似(返回全局變量)

>>> locals(){’__package__’: None, ’__loader__’: <class ’_frozen_importlib.BuiltinImporter’>, ’__doc__’: None, ’__name__’: ’__main__’, ’__builtins__’: <module ’builtins’ (built-in)>, ’__spec__’: None}>>> a = 1>>> locals() # 多了一個key為a值為1的項{’__package__’: None, ’__loader__’: <class ’_frozen_importlib.BuiltinImporter’>, ’a’: 1, ’__doc__’: None, ’__name__’: ’__main__’, ’__builtins__’: <module ’builtins’ (built-in)>, ’__spec__’: None}

2. 可用于函數內。

>>> def f(): print(’before define a ’) print(locals()) #作用域內無變量 a = 1 print(’after define a’) print(locals()) #作用域內有一個a變量,值為1>>> f<function f at 0x03D40588>>>> f()before define a {} after define a{’a’: 1}

3. 返回的字典集合不能修改。

>>> def f(): print(’before define a ’) print(locals()) # 作用域內無變量 a = 1 print(’after define a’) print(locals()) # 作用域內有一個a變量,值為1 b = locals() print(’b['a']: ’,b[’a’]) b[’a’] = 2 # 修改b[’a’]值 print(’change locals value’) print(’b['a']: ’,b[’a’]) print(’a is ’,a) # a的值未變 >>> f()before define a {}after define a{’a’: 1}b['a']: 1change locals valueb['a']: 2a is 1>>>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 瓦房店市| 辽阳市| 靖西县| 渑池县| 巴彦县| 同江市| 精河县| 淮南市| 昌乐县| 大渡口区| 紫金县| 阜新市| 遂川县| 陕西省| 灵武市| 冕宁县| 元谋县| 深圳市| 平阴县| 涡阳县| 曲沃县| 开远市| 济南市| 焦作市| 麻阳| 平罗县| 宁远县| 阿拉善右旗| 卓资县| 茂名市| 托克逊县| 佛冈县| 延津县| 正镶白旗| 定兴县| 禹州市| 眉山市| 灌云县| 天全县| 平度市| 崇义县|