资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

pythonClass:面向对象高级编程__call__&callable()

官网解释:

作为一家“创意+整合+营销”的成都网站建设机构,我们在业内良好的客户口碑。成都创新互联提供从前期的网站品牌分析策划、网站设计、成都网站设计、网站建设、外贸网站建设、创意表现、网页制作、系统开发以及后续网站营销运营等一系列服务,帮助企业打造创新的互联网品牌经营模式与有效的网络营销方法,创造更大的价值。

3.4.5. Emulating callable objects

  • object.__call__(self[, args...])

  • Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1,arg2, ...).

看得懂,但看不明白。。。。。真是硬伤。。。。

这两天发文老是被审核,想不通,是不是我把廖某的全名打出来有人不爽????

为了更好理解,引用廖某的__call__程序与自己的__repr__ / __str__程序做比较:

#廖:__call__此程序的结果是输出输入的name
class Student1(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print('My name is %s.' % self.name)    

h = Student1('MumU')
print 'liao:', h        
           
#我:__repr__ / __str__ 此程序的结果也是输出输入的name
class Student2(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return 'Student2 name:%s'%self.name
    __repr__ = __str__        
    
l = Student2('U')
print 'me:', l

运行结果:

liao: <__main__.Student1 object at 0x7f5fea3108d0>
me: Student2 name:U

嗯,为啥Student1会输出这样呢???

再换一个试法:程序Class都没改,只是输出语句改了(标记:####)

#廖:__call__此程序的结果是输出输入的name
class Student1(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print('My name is %s.' % self.name)    

print Student1('MumU')   ####
           
#我:__repr__ / __str__ 此程序的结果也是输出输入的name
class Student2(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return 'Student2 name:%s'%self.name
    __repr__ = __str__        
    
print Student2('U')  ####

运行结果:

liao: <__main__.Student1 object at 0x7f13dc36b8d0>
me: Student2 name:U

嗯,还是一样,想想也是,因为这输出写法和之前的是等价的。。。。于是我就想,如果__call__是调用自己的函数的话,那么要用函数就得。。。在末尾加()号?!

再试:只改了廖的输出语句(标记:####)

#廖:__call__此程序的结果是输出输入的name
class Student1(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print('My name is %s.' % self.name)    

print Student1('MumU')()   ####
           
#我:__repr__ / __str__ 此程序的结果也是输出输入的name
class Student2(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return 'Student2 name:%s'%self.name
    __repr__ = __str__        
    
print Student2('U')

运行结果:

liao: My name is MumU.
None
me: Student2 name:U

敲你吗,还真是函数。。。。

敲了这么多,蒙了也正常,换个普通的就懂了。

class Animal(object):
    def __init__(self, name):
        self.name = name
    
    def run(self):
        print '%s is running'%self.name
        
dog = Animal('dog')
print '当你要查看class的属性name时:', dog.name
print '当你要查看class的方法(函数)时:', dog.run()

运行结果:

当你要查看class的属性name时: dog
当你要查看class的方法(函数)时: dog is running
None

查看属性是不加()号的, 使用方法(函数)时才需要。

至于为什么运行末尾都有个None??

Because,

dog.run()已经执行了一次print 了,

    def run(self):
        print '%s is running'%self.name

print '当你要查看class的方法(函数)时:', dog.run()

再次执行了一次,所以这个print 就只能输出None了。

我真是太聪明了,哈哈哈哈哈

callable: 查看对象是否可调用,即是否为函数

print 'Student1 可调用??', callable(Student1('MumU'))       
print 'Student2 可调用??', callable(Student2('U'))

运行结果:

Student1 可调用?? True
Student2 可调用?? False

分享名称:pythonClass:面向对象高级编程__call__&callable()
浏览地址:http://cdkjz.cn/article/pdsieg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220