網站首頁 編程語言 正文
一、語法和參數
在Python中evel()函數的語法格式為eval(expression, globals=None, locals=None)
,注意后面還有globals參數和locals參數。eval()函數用于執行一個字符串表達式,并且返回該表達式的值。與eval相近的有exec函數,該函數將會在另一篇文章詳細講解。
- expression:表達式,上面提到evel函數用于執行一個字符串表達式,表達式的內容就放在此處。當表達式涉及到
- globals:該部分必須是字典!必須是字典!必須是字典!否則程序會出錯。當定義了globals 參數之后eval函數的作用域會被限定在globals中。
- locals:該參數掌控局部的命名空間,功能和globals類型,不過當參數沖突時,會執行locals處的參數。
二、expression參數示例
a=10; print(eval("a+1"))
運行結果為11
【解析】:因為此處沒有指定globals和locals,所以直接執行expression部分的內容。該程序的效果等價于a=10 print(a+1)
三、globals參數示例
a=10; g={'a':4} print(eval("a+1",g))
運行結果為5
【解析】:因為現在指定了globals,所以在expression部分的作用域就是globals指定的字典范圍內。所以此時外面的a=10被屏蔽,取用字典中的值。
四、locals參數示例
a=10 b=20 c=30 g={'a':6,'b':8} t={'b':100,'c':10} print(eval('a+b+c',g,t))
運行結果為116
【解析】:根據上面題目的練習我們知道了當有globals和locals時作用的范圍域是在globals和locals中,所以a=10,b=20,c=30不會被應用。a和c的值分別去字典g和字典t中的值,當globals和locals中都有參數b時取locals中的值。所以a=6,b=100,c=10
五、eval函數的危險之處
eval函數非常的方便,我們可以使用一行代碼就實現計算器的功能print(eval(input('請輸入')))。但是因為它具有可以將字符串轉成表達式執行的特性,所以它也就可以去執行系統命令。這樣很容易被別有用心的人用來執行系統命令,刪除關鍵系統文件。
六、eval()函數官方文檔
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can 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 expression normally has full access to the standard builtins module and restricted environments are propagated. If the locals dictionary 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.
附eval()函數常見作用有
1、計算字符串中有效的表達式,并返回結果
>>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> eval("n + 4") 85
2、將字符串轉成相應的對象(如list、tuple、dict和string之間的轉換)
>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>> b = eval(a) >>> b [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> a = "{1:'xx',2:'yy'}" >>> c = eval(a) >>> c {1: 'xx', 2: 'yy'} >>> a = "(1,2,3,4)" >>> d = eval(a) >>> d (1, 2, 3, 4)
3、將利用反引號轉換的字符串再反轉回對象
>>> list1 = [1,2,3,4,5] >>> `list1` '[1, 2, 3, 4, 5]' >>> type(`list1`) <type 'str'> >>> type(eval(`list1`)) <type 'list'> >>> a = eval(`list1`) >>> a [1, 2, 3, 4, 5]
總結
原文鏈接:https://blog.csdn.net/qq_42942881/article/details/109400343
相關推薦
- 2022-03-15 feign.RetryableException: Read timed out executing
- 2021-11-12 C/C++?Qt?StatusBar底部狀態欄應用教程_C 語言
- 2023-06-21 Python生成元組和字典的方法_python
- 2023-02-18 go?gin?正確讀取http?response?body內容并多次使用詳解_Golang
- 2023-07-29 使用 XMLHttpRequest 實現 ajax
- 2022-12-13 iOS底層實例解析Swift閉包及OC閉包_IOS
- 2022-04-23 超出省略號,el-tooltip懸停展示全部的簡單實現
- 2022-07-29 C++超詳細講解智能指針_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支