星寒新影视 发表于 2023-7-26 16:40:27

Python 内置函数 :eval、exec、hash、help、callable

1.eval()

功能描述:“剥去字符串的外衣”,去运行字符串里面的代码
作用 :
(1)参数是一个类似"1+3"这样数学表达式的字符串,可以计算得到返回值(int型)
(2)参数是一个类似"{'name':'tian','age':18}"这样字典、列表、元组外套上一对引号的字符串,可以快速得到字典、列表、元组    
s1 = eval("1+3")
print(s1)
s2 = eval("{'name':'tian','age':18}")
s3 = eval("[, , , , ]")
print(s2,type(s2),s3,type(s3))
#{'name': 'tian', 'age': 18} <class 'dict'> [, , , , ] <class 'list'>2.exec() 功能和eval 类似,只不过能执行更复杂代码返回值为none

s1 = exec("print('nihao')")


s2 = """
a = 1
b = 2
c = 3
sum = a + b + c
print(sum)
"""
exec(s2)

print(exec(s1))
##结果为

6
6
None 3.hash() 获取一个对象的哈希值。 注意对象是不可变类型:int tuple str返回值为哈希值

s1 = hash(1)
s2 = hash("hello world")
s3 = hash("good job")4.help() 函数用于查看函数或模块用途的详细说明。可以传入一个模块名,或者模块名.方法

print(help(exec))
print(help(list.append))5.callable() 判断能否被调用。返回值为True,可调用。返回值为False,不可调用。

#Python小白学习交流群:711312441
s1 ='123'
def func():
   print("hello world")
print(callable(s1))
print(callable(func))
来源:https://www.cnblogs.com/djdjdj123/p/17582707.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Python 内置函数 :eval、exec、hash、help、callable