首页 > 编程语言 > 详细

python 类 __call__

时间:2018-08-31 20:33:00      阅读:174      评论:0      收藏:0      [点我收藏+]

 

__call__ 对象后面加括号,触发执行。

即:对象() 或者 类()()

 

class dog(object):


    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating %s" % (self.name,ss))

    def talk(self):
        print("%s is taking" % self.name)

    def __call__(self, *args, **kwargs):
        print("you are ok?", args, kwargs)



d = dog("jianlin")
d()
d(1,2,3,name="mike")

‘‘‘
you are ok? () {}
you are ok? (1, 2, 3) {‘name‘: ‘mike‘}
‘‘‘

另外一种实现

class dog(object):


    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating %s" % (self.name,ss))

    def talk(self):
        print("%s is taking" % self.name)

    def __call__(self, *args, **kwargs):
        print("you are ok?", args, kwargs)



dog("jianlin")()

# you are ok? () {}

 

python 类 __call__

原文:https://www.cnblogs.com/mingerlcm/p/8470030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!