GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python eval() 函数 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python repr() 函数
Python exec 内置语句

Python eval() 函数

Python 内置函数 Python 内置函数


描述

eval() 函数用来执行一个字符串表达式,并返回表达式的值。

字符串表达式可以包含变量、函数调用、运算符和其他 Python 语法元素。

语法

以下是 eval() 方法的语法:

eval(expression[, globals[, locals]])

参数

  • expression -- 表达式。
  • globals -- 变量作用域,全局命名空间,如果被提供,则必须是一个字典对象。
  • locals -- 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。

eval() 函数将字符串 expression 解析为 Python 表达式,并在指定的命名空间中执行它。

返回值

eval() 函数将字符串转换为相应的对象,并返回表达式的结果。


实例

以下展示了使用 eval() 方法的实例:

实例 1

>>>x = 7 >>> eval( '3 * x' ) 21 >>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> n=81 >>> eval("n + 4") 85

实例 2

# 执行简单的数学表达式
result = eval("2 + 3 * 4")
print(result)  # 输出: 14

# 执行变量引用
x = 10
result = eval("x + 5")
print(result)  # 输出: 15

# 在指定命名空间中执行表达式
namespace = {'a': 2, 'b': 3}
result = eval("a + b", namespace)
print(result)  # 输出: 5

注意: eval() 函数执行的代码具有潜在的安全风险。如果使用不受信任的字符串作为表达式,则可能导致代码注入漏洞,因此,应谨慎使用 eval() 函数,并确保仅执行可信任的字符串表达式。

Python 内置函数 Python 内置函数

AI 思考中...
Python repr() 函数
Python exec 内置语句

7 篇笔记 写笔记

  1. #0

       saberbin

      win***[email protected]

    74

    还可以将字符串对应的名字的变量转换成该变量对应的值:

    >>> x='7'
    >>> x
    '7'
    >>> a=eval(x)
    >>> a
    7
    >>> b='a'     # 等价于b='7'
    >>> c=eval(b)
    >>> c
    7
    >>>
    saberbin

       saberbin

      win***[email protected]

    9年前 (2017-12-22)
  2. #0

       去掉字符串本身的引号

      825***[email protected]

    215

    eval 方法能使字符串本身的引号去掉,保留字符的原本属性。

    >>> a = "123"
    >>> type(a)
    <class 'str'>
    >>> b = eval(a)
    >>> b
    123
    >>> type(b)
    <class 'int'>
    去掉字符串本身的引号

       去掉字符串本身的引号

      825***[email protected]

    9年前 (2018-05-04)
  3. #0

       json

      wrb***[email protected]

    82

    上面笔记中有一句我觉得很在理,eval这个函数就是在返回,它觉得正确的那个值,我这里举两个例子,经验不足,欢迎指正。

    >>>str1 = "[{11,22},{22,33},{33,44},{44,55}]"
    >>>type(str1)
    <class 'str'>
    >>>list1 = eval(str1)
    >>>list1
    [{11,22},{22,33},{33,44},{44,55}]
    >>>type(list)
    <class 'list'>
    >>>a = input("请输入:")
    >>>__import__("os").getcwd()
    >>>eval(a)
    C:\Python27  # 输出结果是保存代码的文件夹路径
    json

       json

      wrb***[email protected]

    9年前 (2018-05-20)
  4. #0

       不梦君

      411***[email protected]

    187

    eval() 函数也可以直接用来提取用户输入的多个值。

    例如:

    a,b=eval(input())

    输入:10,5,得到 a=10,b=5。

    不梦君

       不梦君

      411***[email protected]

    8年前 (2018-09-24)
  5. #0

       李明

      q68***[email protected]

    67

    eval() 函数可将看起来像列表的字符串重新转换为列表。

    例如: 字符串 ['1709020230', '1707030416', '0', '0', '0'] 看起来好像是列表的模样,实际为字符串的字符串还原回列表形态。

    例如:

    zifu=" ['1709020230', '1707030416', '0', '0', '0']  "
    print(type(zifu))
    ls =eval(zifu)
    print(type(ls))

    结果是:

    <class 'str'>
    <class 'list'>

    这个可以应用在例如,读取一个文本文件中的列表,但是直接读取出来的是字符串格式,这时候可以用 eval() 来将字符串格式的“假列表”转换为真列表。

    李明

       李明

      q68***[email protected]

    7年前 (2020-03-18)
  6. #0

       aiF

      173***[email protected]

    34

    eval 函数有将字符格式的伪 python 代码转成正常的 python 代码,特别适合于读取 config 文件和获取用户输入格式的转换:

    config_args = '[(1,2,3),(4,5,6),(7,8,9)]'
    
    # 如果不用 eval 函数的话,将 str 格式转成 python 字面格式需要如下操作
    import re
    config_args = re.findall("\d+",config_args)
    config_args = list(map(int,img_size))
    config_args = [tuple(img_size[i:i+3]) for i in range(0,len(img_size),3)]
    print(config_args)
    
    # 而用来 eval 函数仅仅需要一行代码
    config_args = eval(config_args)
    aiF

       aiF

      173***[email protected]

    5年前 (2022-03-19)
  7. #0

       guorui

      xia***[email protected]

    7

    eval 函数可以用字符串的方式调用函数(也即在当前域取值的作用)

    def handler_version(url):
        pass
    
    def request_version(url):
        pass
    
    func = eval("{0}_version".format(funcType)) # 这里func为相应的函数对象
    url = func(URL) # 通过函数对象调用函数
    guorui

       guorui

      xia***[email protected]

    3年前 (2023-07-17)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.