__getattr__
: 当获取self.__dict__
中没有的键所对应的值的时候,就会调用这个方法因为
parameter, module, buffer
的键值对存在与self._parameters, self._modules, self.buffer
中,所以,当想获取这些 值时, 就会调用这个方法。
def __getattr__(self, name):
if '_parameters' in self.__dict__:
_parameters = self.__dict__['_parameters']
if name in _parameters:
return _parameters[name]
if '_buffers' in self.__dict__:
_buffers = self.__dict__['_buffers']
if name in _buffers:
return _buffers[name]
if '_modules' in self.__dict__:
modules = self.__dict__['_modules']
if name in modules:
return modules[name]
raise AttributeError("'{}' object has no attribute '{}'".format(
type(self).__name__, name))