Python入门-内置对象函数

1.callable()

查看函数知否可调用,可调用返回True,不可用返回False

print("input函数:", callable(input))
#input函数: True

2.eval()

print(eval("3+3"))
# 6


num = 10
s = "加法计算:{}"
var = dict(num =num, info = s)

res = eval("info.format(num *2)", var)
res   
# '加法计算:20'


l = "[1,2,3]"
t = "(1,2,3)"
d = "{1:'one',2:'two',3:'three'}"
res = eval(l)
res2 = eval(t)
res3 = eval(d)
print(res, res2, res3)
print(type(res,),type(res2), type(res3))
"""
[1, 2, 3] (1, 2, 3) {1: 'one', 2: 'two', 3: 'three'}
<class 'list'> <class 'tuple'> <class 'dict'>
"""

3.exec()

num =10
exec("num += 11")
num  
# 21


s = "for i in range(10):"\
    "print(i,end= ',')"
res = exec(s)
print()
print(res)
"""
0,1,2,3,4,5,6,7,8,9,
None
"""

4.compie()

s = "11 + 22 "
code = compile(s, " ","eval")
res = eval(code)
res
# 33

s = "data = input('请输入:')"
code = compile(s,"", "single")
exec(code)

print("输入数据为:", data)
"""
请输入: 99
输入数据为: 99
"""

s = []
data = "for i in range(2):s.append(input('请输入:'))"
code = compile(data, "", "exec")
exec(code)
exec("print('经常访问的地址:',s)")
"""
请输入: baidu
请输入: google
经常访问的地址: ['baidu', 'google']
"""

 

上一篇:C# 标准代码排版样例


下一篇:e开头的