示例一:
class Foo(object): def __init__(self): self.name = '让慕' def show(self): print('show',self.name) # obj = Foo() # Foo.show(obj) #函数,传self obj = Foo() obj.show() #方法,不传self from types import FunctionType,MethodType print(isinstance(Foo.show,FunctionType)) print(isinstance(obj.show,MethodType))
事例二:
#-----------------------方法 class Foo(object): def __init__(self): self.name = '让慕' def show(self): print('show',self.name) list_display = [] def get_list_display(self): self.list_display.append(self.show) return self.list_display obj = Foo() data_list = obj.get_list_display() data_list[0]() #-----------------------函数 class Foo(object): def __init__(self): self.name = '让慕' def show(self): print('show',self.name) list_display = [] def get_list_display(self): #self.list_display[0](self) self.list_display.append(Foo.show) return self.list_display obj = Foo() data_list = obj.get_list_display() data_list[0](obj)