先上一张图,python中内置函数:
python官方解释在这:点我点我
继续聊内置函数:
callable(object):检查对象是否可被调用,或是否可执行,结果为bool值
def f1(): pass f2 = 123 print(callable(f1)) print(callable(f2)) out: True False
char():
ord():
这两个一起讲,都是对应ASCii表的,char(obect)将十进制数字转化为ascii中对应的字母,ord(object)将字母转化为ascii中对应的十进制数字。
顺便上一张ascii表吧,以便以后查询:
用处呢,可以用来搞随机验证码等等,随机验证码在这:点我点我
>>> chr(89) 'Y' >>> chr(64) '@' >>> ord('x') 120 >>>
compile():将字符串编译成python代码,格式:compile( str, file, type )
compile语句是从type类型(包括’eval’: 配合eval使用,’single’: 配合单一语句的exec使用,’exec’: 配合多语句的exec使用)中将str里面的语句创建成代码对象
exam:
>>> s = "print(123)" >>> r = compile(s, "<string>", "exec") >>> exec(r) 123 >>>
执行:
eval() exec()
eval():格式:eval( obj[, globals=globals(), locals=locals()] ), 运算符,表达式:只能执行运算符,表达式, 并且eval() 有返回值
exec(): 格式:exec(obj),执行代码或者字符串,没有返回值,执行代码时,接收代码或者字符串
exam:
>>> s='8*8' >>> eval(s) 64 #有返回值 >>> >>> exec('8+7*8') #无返回 >>> eval('8+7*8') 64 >>>
dir():快速查看对象提供了哪些功能
help():查看对象使用帮助,显示功能详情
exam:
>>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> help(list) Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: