Whether or not an object is a child of a module. Returns ------- bool
(obj: typing.Any, module: types.ModuleType)
| 83 | |
| 84 | |
| 85 | def is_child_of(obj: typing.Any, module: types.ModuleType) -> bool: |
| 86 | """ |
| 87 | Whether or not an object is a child of a module. |
| 88 | |
| 89 | Returns |
| 90 | ------- |
| 91 | bool |
| 92 | """ |
| 93 | if inspect.getmodule(obj) is module: |
| 94 | return True |
| 95 | # Some C modules (e.g. _ast) set __module__ to a different name (e.g. "ast"), |
| 96 | # causing inspect.getmodule() to return a different module object. |
| 97 | # Fall back to checking the module's namespace directly. |
| 98 | obj_name = getattr(obj, "__name__", None) |
| 99 | if obj_name is not None: |
| 100 | return module.__dict__.get(obj_name) is obj |
| 101 | return False |
| 102 | |
| 103 | |
| 104 | def iter_modules() -> "Iterable[types.ModuleType]": |