区分函数和方法的区别

函数,方法 别再傻傻分不清

from types import MethodType(方法),FunctionType(函数)
def func():
    pass
print(isinstance(func,FunctionType))  # True


class A():
    def aaa(self):
        pass
    print(isinstance(aaa,FunctionType))  # True
a=A()
print(isinstance(a.aaa,MethodType))  # True要有实例化出来的对象才可以,如果只是用类名去调用的话还是function,只有用实例化出来的对象去调用才可以得到method

我们的函数只有跟我们的实例化出来的对象有绑定关系才能称之为方法,否则都是函数,即便它是写到类里面的方法,没有跟我们的类实例化出来的对象进行绑定,它依然是函数,而不是类里面的方法.

猜你喜欢

转载自www.cnblogs.com/zjchao/p/9088116.html