callable,compile,eval,single,exec,递归
来源:互联网 发布:js数组定义的方式 编辑:程序博客网 时间:2024/06/04 17:41
callable()是一个布尔函数,确定一个对象是否可以通过函数操作符(())来调用。如果函数可调用则返回true,否则返回false。
1
2
3
4
5
6
7
8
9
10
11
12
>>>
callable
(
dir
)
True
>>>
callable
(
1
)
False
>>>
def
foo():
pass
>>>
callable
(foo)
True
compile()函数允许程序员在运行时刻迅速生成代码对象,然后就可以用exec语句或者内建函数eval()来执行这些对象或者对它们进行求值。一个很重要的观点是:exec和eval都可以执行字符串格式的python代码。当执行字符串形式的代码时,每次都必须对这些代码进行字节编译处理。compile函数提供了一次性字节代码预编译,以后每次调用的时候,都不用编译了。
compile 的三个参数都是必需的,第一参数代表了要编译的python 代码。第二个字符串,虽然是必需的,但通常被置为空串。该参数代表了存放代码对象的文件的名字(字符串类型)。compile 的通常用法是动态生成字符串形式的Python 代码, 然后生成一个代码对象——代码显然没有存放在任何文件。
最后的参数是个字符串,它用来表明代码对象的类型。有三个可能值:
'eval' 可求值的表达式[和eval()一起使用]
'single' 单一可执行语句[和exec 一起使用]
'exec' 可执行语句组[和exec 一起使用]
可求值表达式:
1
2
3
>>> eval_code
=
compile
(
"100+200"
,"
","
eval
")
>>>
eval
(eval_code)
300
1
2
3
4
5
>>> single_code
=
compile
(
"print 'hello world!'"
,"
", "
single")
>>> single_code
<code
object
<module> at
021174E8
,
file
"", line
1
>
>>>
exec
single_code
hello world!
1
2
3
4
5
exec_code
=
compile
(
"""
req = input("count how many numbers?")
for eachNum in range(req):
print eachNum,
"""
,"
","
exec
")
1
2
3
>>>
exec
exec_code
count how many numbers?
6
0
1
2
3
4
5
exec通常直接执行一个字符串,eval()通常执行的是表达式。
递归(函数)
递归可以用数学中累加解释1+2+···+n,要把所有可能都想到(n>=1)反回return不能使用表达式。
def foo(n):
return n+(n-1)
0 0
- callable,compile,eval,single,exec,递归
- eval+exec+compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- python中eval, exec, execfile,和compile
- Python exec eval execfile compile 学习
- python中eval, exec, execfile,和compile
- python:exec eval execfile compile介绍
- python:exec eval execfile compile介绍
- python:exec eval execfile compile介绍
- python中eval, exec, execfile,和compile
- Python中的eval(),exec,execfile(),compile()
- 备忘-设计模式
- C# 多文件重命名思路及实现 下:实例
- iOS中出现循环引用时常见的三种情况
- 朱友鹏老师嵌入式课程观后感
- Android:Handler+Looper+MessageQueue+Thread(线程间的通信)随记
- callable,compile,eval,single,exec,递归
- 优化SQLServer数据库加快查询速度
- 天声人語 20151219 手書きの年賀状
- C++类模板的定义
- 堆排序
- 日经春秋 20151219
- PHP常用正则表达式汇总
- juery实现粘贴复制
- win7+Ubuntu14.10双系统重装win10后,通过easyBCD2.0在win10下恢复ubuntu的引导