python预编译函数compile,exec,eval
funcname = "func"
func = "def %s():\n" % funcname
funccontent = 'print "hello,world"'
func += funccontent
evalcode = compile(func, '', 'eval')
exec (evalcode)
eval("%s" % funcname)
执行后编译错误:
eval_code = compile(func, '', 'eval')
File "", line 1
def func():
^
SyntaxError: invalid syntax
报错后使用 exec :
funcname = "func"
func = "def %s():\n" % funcname
funccontent = 'print "hello,world"'
func += funccontent
evalcode = compile(func, '', 'exec')
exec (evalcode)
eval("%s" % funcname)
执行后编译错误:
Traceback (most recent call last):
File "/tmp/417881432/main.py", line 5, in <module>
evalcode = compile(func, '', 'exec')
File "", line 2
print "hello,world"
^
IndentationError: expected an indented block
exit status 1
修改缩进
funcname = "func"
func = "def %s():\n" % funcname
funccontent = ' print "hello,world"'
func += funccontent
evalcode = compile(func, '', 'exec')
exec (evalcode)
eval("%s" % funcname)
运行成功
python预编译函数compile,exec,eval的更多相关文章
- Python中动态编译函数compile(source, filename, mode, ......)参数filename的作用是什么?
动态编译函数compile调用语法如下: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 其中的fi ...
- python 内置函数 : compile()
这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. 参数source是一串字符串的源码,或者是 ...
- python内置函数-compile()
python的内置函数 compile()--编译. 这个函数有什么用呢? 一个最简单的例子, 就是我们的代码, 会被解释器读取,解释器读取后的其实是字符串, 然后通过compile编译后, 又转换成 ...
- Python内置函数compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- Python预编译语句防止SQL注入
这个月太忙,最近不太太平,我的愿望是世界和平! ================================== 今天也在找python的预编译,早上写的sql是拼接来构成的.于是找了2篇文章,还 ...
- Python中两大神器&exec() &eval()
一.神器1 -- 内置函数eval eval是python中的内置函数,它的作用是将字符串变为所对应的表达式,也相当于一个功能代码加双引号变为字符串,而eval又将字符串转为相应的功能,它在使用过程中 ...
- 第11.25节 Python正则表达式编译re.compile及正则对象使用
一. 引言 在<第11.2节 Python 正则表达式支持函数概览>介绍了re模块的主要函数,在<第11.3节 Python正则表达式搜索支持函数search.match.fullm ...
- Python内置函数(62)——exec
英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...
- Python内置函数(20)——exec
英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...
随机推荐
- VS挂机移动鼠标代码
#include <time.h> #include <stdio.h> #include <Windows.h> HANDLE ghMutex; //////// ...
- vim 使用学习操作
1 跳转 命令 作用 h 光标向左移动 l 光标向右移动 j 光标向上移动 k 光标向下移动 w 移动光标到下一个单词开头. e 移动光标到下一个单词结尾 b 移动光标到上一个单词. 0 移动光标到本 ...
- jinja2
本文转自:https://www.cnblogs.com/dachenzi/p/8242713.html 模板 要了解jinja2,那么需要先理解模板的概念.模板在Python的web开发中广泛使用, ...
- springboot+freemarker毕业设计项目错误合集
1.springboot的主程序类必须在最外层. 换句话说,报错: This application has no explicit mapping for /error, so you are se ...
- IntelliJ IDE 基础经验备案
1.配置本地的JAVA环境 2.配置本地安装的Maven环境 详情 1.配置本地的JAVA环境 准备: 本地已经安装java环境,目录:C:\Program Files\Java\jdk1.8.0_1 ...
- 四、OpenStack—glance组件介绍与安装
一.glance介绍 Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚拟机镜像的metadata及 ...
- 福州大学软件工程1916|W班 第6次作业成绩排名
作业链接 团队第三次-项目原型设计 评分细则 博客评分标准 在随笔开头,备注小组同学的学号.(1') 文字准确.样式清晰.图文并茂.字数在1000字左右.(10') 原型模型必须采用专用的原型模型设计 ...
- C++ 使用 curl 进行 http 请求(GET、POST、Download)的封装
修改自网路 CommonTools.h /* * CommonTools.h * * Created on: 2018年8月2日 * Author: didi */ #include <iost ...
- 8080端口被System占用
System是Windows页面内存管理进程,拥有0级优先权,没有它系统无法启动 就是说,System进程是无法关闭的,所以不要尝试去强行关闭,可能引起电脑异常查看是否是IIS占用的, 进入电脑控制面 ...
- 在github上面创建新的分支
第一步:git branch 查看当前分支情况 git branch //查看当前分支情况 第二步:git branch 分支名,新建一个自己的分支 git branch 分支名 // 新建一个自己的 ...