Last active
August 29, 2015 14:00
-
-
Save dutc/11156722 to your computer and use it in GitHub Desktop.
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
# only works for kwargs | |
class autoinit(type): | |
def __call__(self, *args, **kwargs): | |
rv = super().__call__(*args, **kwargs) | |
for k,v in rv.__init__.__annotations__.items(): | |
setattr(rv, k, eval(v, kwargs)) | |
return rv | |
class Foo(metaclass=autoinit): | |
def __init__(self, x: 'x + y', y: 'y * 10', z): | |
print('Foo.__init__', x, y, z) | |
if __name__ == '__main__': | |
foo = Foo(x = 10, y = 20, z = 30) | |
print(foo.x, foo.y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment