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 ...
随机推荐
- Nginx+DNS负载均衡实现
负载均衡有多种实现方法,nginx.apache.LVS.F5硬件.DNS等. DNS的负载均衡就是一个域名指向多个ip地址,客户访问的时候进行轮询解析 操作方法,在域名服务商解析的DNS也可以是第三 ...
- C# BackJson Beautiful format
using System; using System.Collections.Generic; using System.Linq; using System.Web; public class ...
- 【spring】-- springboot配置全局异常处理器
一.为什么要使用全局异常处理器? 什么是全局异常处理器? 就是把错误异常统一处理的方法. 应用场景: 1.当你使用jsr303参数校验器,如果参数校验不通过会抛异常,而且无法使用try-catch语句 ...
- nginx.conf 中php-ftp配置
location ~ .php$ { root /home/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_par ...
- Pandas 0 数据结构Series
# -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...
- Python 3.5 filter
filter(F, L) F: 函数.L:范围 filter的功能是:用函数F把L范围内的参数做过滤 通常和list一起使用,把过滤后的参数做成列表 list(filter(lambda n:not ...
- ZOJ4043 : Virtual Singers
将所有$A$和$B$混在一起排序,那么每个$B$要匹配一个$A$,从左往右依次考虑每个数: 如果是一个$B$: 如果左边没有多余的$A$,那么将其放入堆$q_C$中,表示这个$B$还未匹配. 否则选择 ...
- IndentityServer4
官网: https://identityserver4.readthedocs.io/en/latest/index.html 比较好的中文博客: 晓晨Master: https://www.cnbl ...
- CommonsChunkPlugin
CommonsChunk 插件的作用就是提取代码中的公共代码,然后将公共模块打包到一个独立的文件中,以便在其它的入口和模块中使用,原理就是把多个入口共同的依赖都给定义成一个新入口 多种打包情况: 单一 ...
- js的一些function
/** * * 根据秒数返回 一个日期范围 * timerFilter(10) */ function timerFilter(n) { let days = 31; // 一月多少天 const o ...