def add_link(self, name, link):
"""Registers a child link to this chain.
The registered link is saved and loaded on serialization and
deserialization, and involved in the optimization. The registered link
is called a child. The child link is set to an attribute of the chain
with the given name.
This method also sets the :attr:`~Link.name` attribute of the
registered link. If the given link already has the name attribute set,
then it raises an error.
Args:
name (str): Name of the child link. This name is also used as the
attribute name.
link (Link): The link object to be registered.
"""
if link.name is not None:
raise ValueError(
'given link is already registered to another chain by name %s'
% link.name)
d = self.__dict__
if name in d:
raise AttributeError(
'cannot register a new link %s: attribute exists' % name)
self._children.append(name)
link.name = name
d[name] = link
评论列表
文章目录