Created
May 8, 2021 08:33
-
-
Save rupython/1deab94145e93b91af10d3f1b0dd6f96 to your computer and use it in GitHub Desktop.
From: Павел
This file contains 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 [14]: class LazyInit(type): | |
...: def __new__(mcs, name, bases, namespace): | |
...: namespace["__init__"] = lambda self, *args, **kwargs: vars(self).update(kwargs) | |
...: return super().__new__(mcs, name, bases, namespace) | |
...: | |
In [15]: class Foo(metaclass=LazyInit): | |
...: def __init__(self, name): | |
...: pass | |
...: | |
In [16]: obj = Foo(name="John") | |
In [17]: obj.name | |
Out[17]: 'John' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment