码迷,mamicode.com
首页 > 其他好文 > 详细

类的方法和函数

时间:2017-10-20 20:07:40      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:cti   types   elf   turn   show   end   int   --   nbsp   

 

示例一:

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)

 

类的方法和函数

标签:cti   types   elf   turn   show   end   int   --   nbsp   

原文地址:http://www.cnblogs.com/zhaochangbo/p/7701018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!