(1)看是否存在对应的魔术方法。例如,len() 是一个内置函数,因为它实际调用的是魔术方法__len__() ;还有常用的iter(),它调用的是__iter__() ,所以也是内置函数;而因为不存在 __range__() 魔术方法
(2)使用 type() 进行判断,结果为 builtin_function_or_method 的才是内置函数。
print(type(len))
结果:<class 'builtin_function_or_method'>
print(type(open))
结果:<class 'builtin_function_or_method'>
print(type(range))
结果:
print(type(ennumerate))
结果:<class 'type'>
print(type(range))
结果:<class 'type'>
print(type(enumerate))
结果:<class 'type'>
print(type(str))
结果:<class 'type'>
print(type(list))
结果:<class 'type'>
本文详细解析了Python中内置函数的识别方法,包括通过检查魔术方法的存在与否以及使用type()函数来判断函数类型,帮助读者深入理解Python的内部机制。
855

被折叠的 条评论
为什么被折叠?



