Skip to content

Instantly share code, notes, and snippets.

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