av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python使用eval函數(shù)執(zhí)行動(dòng)態(tài)標(biāo)表達(dá)式過(guò)程詳解

瀏覽:3日期: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.

執(zhí)行動(dòng)態(tài)標(biāo)表達(dá)式求值

說(shuō)明:

1. 執(zhí)行動(dòng)態(tài)語(yǔ)句,返回語(yǔ)句執(zhí)行的值。

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

2. 第一個(gè)參數(shù)為語(yǔ)句字符串,globals參數(shù)和locals參數(shù)為可選參數(shù),如果提供,globals參數(shù)必需是字典,locals參數(shù)為mapping對(duì)象。

3. globals參數(shù)用來(lái)指定代碼執(zhí)行時(shí)可以使用的全局變量以及收集代碼執(zhí)行后的全局變量。

>>> 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,可執(zhí)行4

4. locals參數(shù)用來(lái)指定代碼執(zhí)行時(shí)可以使用的局部變量以及收集代碼執(zhí)行后的局部變量

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

5. 為了保證代碼成功運(yùn)行,globals參數(shù)字典不包含 __builtins__ 這個(gè) key 時(shí),Python會(huì)自動(dòng)添加一個(gè)key為 __builtins__ ,value為builtins模塊的引用。如果確實(shí)要限制代碼不使用builtins模塊,需要在global添加一個(gè)key為_(kāi)_builtins__,value為{}的項(xiàng)即可(很少有人這么干吧)。

>>> g = {}>>> eval(’abs(-1)’,g)1>>> g = {’__builtins__’:{}}>>> eval(’abs(-1)’,g) #不能使用內(nèi)置函數(shù)了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. 當(dāng)globals參數(shù)不提供是,Python默認(rèn)使用globals()函數(shù)返回的字典去調(diào)用。當(dāng)locals參數(shù)不提供時(shí),默認(rèn)使用globals參數(shù)去調(diào)用。

>>> 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參數(shù)未提供,locals參數(shù)=globals參數(shù)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參數(shù)含有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)>}>>>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日韩精品免费播放 | 精品国产不卡一区二区三区 | 国产成人精品久久 | 久久www免费视频 | 在线成人一区 | 一区二区日韩精品 | 成人在线免费电影 | 国产精品中文字幕在线 | 久久久久国产一区二区 | 久草久草久草 | 黄色片免费看 | 亚洲一区二区三区在线免费 | 韩国av影院| 欧美日韩精品 | 欧美一级做性受免费大片免费 | 久久蜜桃av一区二区天堂 | 亚洲天堂av网 | 免费黄色网址视频 | 老司机成人在线 | 国产精品久久 | 精品国产1区2区3区 一区二区手机在线 | 欧美日韩亚洲视频 | 久久久久久国产精品免费免费狐狸 | aaaaaa大片免费看最大的 | 国产精品女人久久久 | 午夜爱爱毛片xxxx视频免费看 | h视频在线免费观看 | 国产精品99久久久久久久久久久久 | 欧美色综合网 | 精品久久久久久久久久 | www视频在线观看 | 羞羞涩涩在线观看 | 本道综合精品 | 午夜影视免费片在线观看 | 欧美激情综合 | 欧美一级欧美一级在线播放 | 免费一级黄色电影 | 午夜亚洲| 久久99国产精品 | 日韩中文字幕一区 | 成人av一区二区在线观看 |