| 114 | yield m |
| 115 | |
| 116 | def named_modules_with_parent(self, memo=None, prefix="", parent=None, remove_duplicate=True): |
| 117 | if memo is None: |
| 118 | memo = set() |
| 119 | if self not in memo: |
| 120 | if remove_duplicate: |
| 121 | memo.add(self) |
| 122 | yield prefix, self, parent |
| 123 | |
| 124 | if parent: |
| 125 | # Use the up-to-date module from the parent, to allow replacing |
| 126 | # layers while iterating this generator. |
| 127 | module_name = prefix.rsplit(".", 1)[-1] |
| 128 | module = getattr(parent, module_name) |
| 129 | if module is None: |
| 130 | return |
| 131 | else: |
| 132 | module = self |
| 133 | |
| 134 | for child_name, child_module in module._modules.items(): |
| 135 | if child_module is None: |
| 136 | continue |
| 137 | submodule_prefix = prefix + ("." if prefix else "") + child_name |
| 138 | for m in child_module.named_modules_with_parent( |
| 139 | memo, submodule_prefix, module, remove_duplicate |
| 140 | ): |
| 141 | yield m |
| 142 | |
| 143 | def named_children(self): |
| 144 | memo = set() |