亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

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

Python使用eval函數執行動態標表達式過程詳解

瀏覽:10日期:2022-07-08 08:35:53

英文文檔:

eval(expression, globals=None, locals=None)The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, localscan be any mapping object.

The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed. This means that expressionnormally has full access to the standard builtins module and restricted environments are propagated. If the localsdictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:

>>> x = 1>>> eval(’x+1’)2

This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string. If the code object has been compiled with ’exec’ as the mode argument, eval()‘s return value will be None.

Hints: dynamic execution of statements is supported by the exec() function. The globals() and locals() functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or exec().See ast.literal_eval() for a function that can safely evaluate strings with expressions containing only literals.

執行動態標表達式求值

說明:

1. 執行動態語句,返回語句執行的值。

>>> eval(’1+2+3+4’)10

2. 第一個參數為語句字符串,globals參數和locals參數為可選參數,如果提供,globals參數必需是字典,locals參數為mapping對象。

3. globals參數用來指定代碼執行時可以使用的全局變量以及收集代碼執行后的全局變量。

>>> g = {’num’:2}>>> eval(’num + 2’) #num未定義Traceback (most recent call last): File '<pyshell#3>', line 1, in <module> eval(’num + 2’) File '<string>', line 1, in <module>NameError: name ’num’ is not defined>>> eval(’num + 2’,g) #g中有定義num,可執行4

4. locals參數用來指定代碼執行時可以使用的局部變量以及收集代碼執行后的局部變量

>>> g = {’num1’:2}>>> l = {’num2’:4}>>> eval(’num1+num2’,g,l)6

5. 為了保證代碼成功運行,globals參數字典不包含 __builtins__ 這個 key 時,Python會自動添加一個key為 __builtins__ ,value為builtins模塊的引用。如果確實要限制代碼不使用builtins模塊,需要在global添加一個key為__builtins__,value為{}的項即可(很少有人這么干吧)。

>>> g = {}>>> eval(’abs(-1)’,g)1>>> g = {’__builtins__’:{}}>>> eval(’abs(-1)’,g) #不能使用內置函數了Traceback (most recent call last): File '<pyshell#9>', line 1, in <module> eval(’abs(-1)’,g) File '<string>', line 1, in <module>NameError: name ’abs’ is not defined

6. 當globals參數不提供是,Python默認使用globals()函數返回的字典去調用。當locals參數不提供時,默認使用globals參數去調用。

>>> num = 1>>> eval(’num+2’)3>>> globals() #返回字典中含有num的key{’__doc__’: None, ’num’: 1, ’__package__’: None, ’__name__’: ’__main__’, ’__loader__’: <class ’_frozen_importlib.BuiltinImporter’>, ’__spec__’: None, ’__builtins__’: <module ’builtins’ (built-in)>}>>> eval(’num+2’,{}) #locals參數未提供,locals參數=globals參數Traceback (most recent call last): File '<pyshell#3>', line 1, in <module> eval(’num+2’,{}) File '<string>', line 1, in <module>NameError: name ’num’ is not defined>>> l = locals() >>> eval(’num+2’,{},l) #locals參數含有num的key,能求值3>>> locals(){’__doc__’: None, ’l’: {...}, ’num’: 1, ’__package__’: None, ’__name__’: ’__main__’, ’__loader__’: <class ’_frozen_importlib.BuiltinImporter’>, ’__spec__’: None, ’__builtins__’: <module ’builtins’ (built-in)>}>>>

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 美女被靠视频免费网站不需要会员 | 三级色网| 国产欧美综合在线一区二区三区 | 欧美+日本+国产+在线观看 | 老鸭窝 国产 精品 91 | 成人韩免费网站 | 99久久精品免费看国产 | 国产精品免费观在线 | 99在线视频免费 | 三级视频在线观看 | 无毛片| 免费真实播放国产乱子伦 | 高清视频 一区二区三区四区 | 国产日产欧产精品精品推荐小说 | 欧美在线看欧美高清视频免费 | 日本丶国产丶欧美色综合 | 欧美日韩精品一区二区三区视频播放 | fc2成年手机免费共享视频 | 爽爽窝窝午夜精品一区二区 | 欧美不卡视频 | 欧美视频一区二区专区 | 超清首页 国产 亚洲 丝袜 | 亚洲网站在线观看 | 中国一级毛片欧美一级毛片 | 国产精品久久人人做人人爽 | 日本一级特黄在线播放 | 欧美一级久久久久久久大 | 草草影院地址 | 鸥美毛片 | 亚洲国产欧美精品一区二区三区 | 欧美激情一级欧美精品 | 午夜欧美成人久久久久久 | 一级做a爰性色毛片 | 国内久久精品视频 | 久久久久亚洲精品影视 | 国产或人精品日本亚洲77美色 | 亚洲精品一级片 | 国产在线观看免费视频软件 | 中文字幕一区二区三区有限公司 | 国产成人精品免费视 | 欧美人体在线 |