__all__ — Advanced Examples
List of public names exported by a module with 'from module import *'
Advanced __all__ patterns
Advanced usage patterns with __all__.
python
# Advanced __all__ patterns class AutoInit: """Automatically initialize attributes from __init__ parameters.""" def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) import inspect init = cls.__dict__.get("__init__") if init: params = list(inspect.signature(init).parameters.keys())[1:] original_init = init def new_init(self, *args, **kw): original_init(self, *args, **kw) cls.__init__ = new_init class Point(AutoInit): def __init__(self, x, y): self.x = x self.y = y p = Point(3, 4) print(vars(p))
Advanced patterns with __all__ enable powerful metaprogramming techniques.
Want to try these examples interactively?
Open Advanced Playground