类外面: ---- 函数

类里面
  取决调用者
  类.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

 

相关文章:

  • 2021-07-19
  • 2021-07-25
  • 2021-06-02
  • 2021-12-19
  • 2021-10-23
  • 2021-06-18
  • 2022-12-23
  • 2021-09-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-06-28
  • 2021-07-03
相关资源
相似解决方案