Python考试题--第56道题目--怎样区分调用的是手段还是函数--2022年04月11日
原创目录
1,可以直接使用type函数返回类型,确定它是方法还是函数。
1.结构和功能:
1,分别构造一个方法和一个函数,其名称为process。
2该方法实际上是类下面的一个函数。
3、pass是创建一个占位符,pass该语句不执行任何操作。
# 方法
class Myclass:
def process():
pass
# 函数
def process():
pass
2.判断是方法还是函数:
1,可以直接使用type函数返回类型,确定它是方法还是函数。
print(type(Myclass().process))
print(type(process))
#
#
print(type(Myclass().process).__name__ == method)
print(type(process).__name__ == function)
# True
# True
2、使用isinstance()功能判断。
from types import MethodType, FunctionType
print(Myclass().process is methodtype?, isinstance(Myclass().process, MethodType))
print(Myclass().process is functiontype?, isinstance(Myclass().process, FunctionType))
print(process is methodtype?, isinstance(process, MethodType))
print(process is functiontype?, isinstance(process, FunctionType))
# 通过isinstance函数可以确定是否调用了方法或函数。
# 如果它是方法,则类型为MethodType;如果是函数,则类型为FunctionType。
结果如下:

版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
itfan123




