Skip to content

Instantly share code, notes, and snippets.

@rupython
Created May 8, 2021 08:10
Show Gist options
  • Save rupython/9260e3b65015e8847b90657db9008e20 to your computer and use it in GitHub Desktop.
Save rupython/9260e3b65015e8847b90657db9008e20 to your computer and use it in GitHub Desktop.
From: Павел
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