pyhon----函数和方法的关系

1、如果使用类名调用,为函数,需要手动传self

2、如果使用对象调用,为方法,不用手动传self

class Foo(object):
    def __init__(self):
        self.name="haiyan"
    def func(self):
        print(self.name)

obj = Foo()
obj.func()
Foo.func(obj)

  

from types import FunctionType,MethodType
obj = Foo()
print(isinstance(obj.func,FunctionType))  #False
print(isinstance(obj.func,MethodType))   #True   #说明这是一个方法

print(isinstance(Foo.func,FunctionType))  #True   #说明这是一个函数。
print(isinstance(Foo.func,MethodType))  #False

  

猜你喜欢

转载自www.cnblogs.com/yanxiaoge/p/10630981.html