Python之exec()/compile()方法使用
2017-07-03 15:16
120 查看
# Python内置函数exec()可以用来执行Python代码 # 或内置函数compile()编译的代码对象 # exec程序中可写入python语法格式的代码,并直接输出。 exec('print("Hello World!")') # compile(source, filename, mode[, flags[, dont_inherit]]) # 中文说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。 # 参数source:字符串或者AST(Abstract Syntax Trees)对象。 # 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。 # 参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。 # 参数flag和dont_inherit:这两个参数暂不介绍,可选参数。 code = "for i in range(0, 10): print(i)" cmpcode = compile(code, '', 'exec') exec(cmpcode)
运行结果
相关文章推荐
- Boost Python中exec,eval, exec_file, extract<>的函数形式和使用方法
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
- 【用Python写爬虫】获取html的方法【三】:使用cPAMIE
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- python中datetime的比较方法使用
- 使用Runtime.getRuntime().exec()方法时要注意Working Directory
- python中eval, exec, execfile,和compile
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- [例子]Display syncExec() asyncExec() 方法的使用
- 动态语句的使用方法(exec/sp_executesql)
- 升级Python导致Yum服务不能使用的修复方法
- [Dynamic Language] Python Exec & Compile
- python下使用qrcode方法
- Python __getattr__与__setattr__使用方法
- Python httplib,smtplib使用方法
- Python strip lstrip rstrip使用方法
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- Python strip lstrip rstrip使用方法
- python中eval, exec, execfile,和compile
- Linux find exec的使用方法