设为首页 加入收藏

TOP

Python 内置函数 :eva l、exec、hash、help、callable
2023-08-06 07:49:54 】 浏览:85
Tags:Python eva exec hash help callable

1.eva l()

功能描述:“剥去字符串的外衣”,去运行字符串里面的代码

作用 :
(1)参数是一个类似"1+3"这样数学表达式的字符串,可以计算得到返回值(int型)

(2)参数是一个类似"{'name':'tian','age':18}"这样字典、列表、元组外套上一对引号的字符串,可以快速得到字典、列表、元组    

s1 = eva l("1+3")
print(s1) 
s2 = eva l("{'name':'tian','age':18}") 
s3 = eva l("[[1,2], [3,4], [5,6], [7,8], [9,0]]")
print(s2,type(s2),s3,type(s3))
#{'name': 'tian', 'age': 18} <class 'dict'> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] <class 'list'>

2.exec() 功能和eva l 类似,只不过能执行更复杂代码 返回值为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))
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇高效Python-1提高数据处理效率的.. 下一篇python解释器及开发工具安装-wind..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目