类和函数的区别

类外面: ---- 函数

类里面
  取决调用者
  类.func(xx) ---- 函数
  对象.func() ----方法

可以使用 MethodType, FunctionType来判断:

from types import MethodType, FunctionType


class A:

    def func(self):
        return 123


print(isinstance(A.func, MethodType))       # False
print(isinstance(A.func, FunctionType))     # True          类调用,是函数

a = A()

print(isinstance(a.func, MethodType))       # True          对象调用, 是方法
print(isinstance(a.func, FunctionType))     # False
复制代码

 

posted @ 2018-11-27 10:39  金元  阅读(951)  评论(0编辑  收藏  举报