1. 程式人生 > >類的方法和函數

類的方法和函數

cti append display return sta brush eth sel func

示例一:

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)

類的方法和函數