Created
May 8, 2021 08:10
-
-
Save rupython/9260e3b65015e8847b90657db9008e20 to your computer and use it in GitHub Desktop.
From: Павел
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: class LazyInit: | |
...: def __new__(cls, **kwargs): | |
...: instance = super().__new__(cls) | |
...: for t in instance.__init__.__code__.co_varnames[1:]: | |
...: setattr(instance, t, kwargs[t]) | |
...: return instance | |
...: | |
In [2]: class Foo(LazyInit): | |
...: def __init__(self, name): | |
...: pass | |
...: | |
In [3]: obj = Foo(name="John") | |
In [4]: obj.name | |
Out[4]: 'John' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment