1.Python super() 函数
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print ('Parent')
def bar(self,message):
print ("%s from Parent" % message)
class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child')
def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
# 执行结果
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.
np.array可以将输入的数据转换为数组
2.
class Student:
def __call__(self, param):
print('I can called like a function')
print('传入参数的类型是:{} 值为: {}'.format(type(param), param))
res = self.forward(param)
return res
def forward(self, input_):
print('forward 函数被调用了')
print('in forward, 传入参数类型是:{} 值为: {}'.format(type(input_), input_))
return input_
a = Student()
input_param = a('data')
print("对象a传入的参数是:", input_param)
# 输出结果
11111111
I can called like a function
传入参数的类型是:<class 'str'> 值为: data
forward 函数被调用了
in forward, 传入参数类型是:<class 'str'> 值为: data
对象a传入的参数是: data
model(data)等价于model.forward(data),是因为__call__函数中调用了forward函数。
forward函数在实例化对象的时候调用。