类和函数的区别

类外面: ---- 函数

类里面
  取决调用者
  类.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 @ 2017-10-22 18:22  静静别跑  阅读(322)  评论(0编辑  收藏  举报