Python中把类的实例变成一个可调用对象(__call__)

Python中把类的实例变成一个可调用对象(__call__)

__call__
 

在Python中,函数其实是一个对象:

>>> f = abs
>>> f.__name__
'abs'
>>> f(-123)
 

由于 f 可以被调用,所以,f 被称为可调用对象。

所有的函数都是可调用对象。

一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法call()。

我们把 Person 类变成一个可调用对象:

class Person(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

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

 

现在可以对 Person 实例直接调用:

>>> p = Person('Bob', 'male')
>>> p('Tim')
My name is Bob...
My friend is Tim...
 

单看 p(‘Tim’) 你无法确定 p 是一个函数还是一个类实例,所以,在Python中,函数也是对象,对象和函数的区别并不显著。

任务
改进一下前面定义的斐波那契数列:

class Fib(object):
    ???
 

请加一个__call__方法,让调用更简单:

>>> f = Fib()
>>> print f(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
 

可以把实例对象用类似函数的形式表示,进一步模糊了函数和对象之间的概念

class Fib(object):
    def __init__(self):
        pass
    def __call__(self,num):
        a,b = 0,1;
        self.l=[]

        for i in range (num):
            self.l.append(a)
            a,b= b,a+b
        return self.l
    def __str__(self):
        return str(self.l)
    __rept__=__str__

f = Fib()
print f(10)

# 以下内容为自己的一点想法
class VGGNet(fluid.dygraph.Layer):
    '''
    VGG网络
    '''
    def __init__(self):
        super(VGGNet, self).__init__()
        # 3:C, 64 核数,3 size,, 2 poolsize,2 步长 2: 连续卷积个数  
        self.convpool_1 = ConvPool(3, 64, 3, 2, 2, 2, act="relu")
        self.convpool_2 = ConvPool(64, 128, 3, 2, 2, 2, act="relu")
        self.convpool_3 = ConvPool(128, 256, 3, 2, 2, 3, act="relu")
        self.convpool_4 = ConvPool(256, 512, 3, 2, 2, 3, act="relu")
        self.convpool_5 = ConvPool(512, 512, 3, 2, 2, 3, act="relu")
        
        self.pool_5_shape = 512 * 7 * 7
        self.fc_1 = fluid.dygraph.Linear(self.pool_5_shape, 4096, act="relu")
        self.fc_2 = fluid.dygraph.Linear(4096, 4096, act="relu")
        self.fc_3 = fluid.dygraph.Linear(4096, 2, act="softmax")

    def forward(self, inputs, label=None):
        """前向计算"""
        out = self.convpool_1(inputs)
        out = self.convpool_2(out)
        out = self.convpool_3(out)
        out = self.convpool_4(out)
        out = self.convpool_5(out)

        out = fluid.layers.reshape(out, shape=[-1, 512*7*7])
        out = self.fc_1(out)
        out = self.fc_2(out)
        out = self.fc_3(out)

        if label is not None:
            acc = fluid.layers.accuracy(input=out, label=label)
            return out, acc
        else:
            return out

  

vgg = VGGNet()

out , acc = vgg(image, label)

应该是一样的道理

 
posted @ 2020-04-03 23:50  smagic  阅读(439)  评论(0)    收藏  举报