区分函数和方法的区别

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

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
 咱们的函数只有跟咱们的实例化出来的对象有绑定关系才能称之为方法,不然都是函数,即使它是写到类里面的方法,没有跟咱们的类实例化出来的对象进行绑定,它依然是函数,而不是类里面的方法.
相关文章
相关标签/搜索