Skip to content

Instantly share code, notes, and snippets.

@dutc
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dutc/11156722 to your computer and use it in GitHub Desktop.
Save dutc/11156722 to your computer and use it in GitHub Desktop.
# 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