Skip to content

Instantly share code, notes, and snippets.

@milesrout
Created September 5, 2016 11:21
Show Gist options
  • Save milesrout/dc034232e03c1753a80fe9954adcbd0c to your computer and use it in GitHub Desktop.
Save milesrout/dc034232e03c1753a80fe9954adcbd0c to your computer and use it in GitHub Desktop.
import functools, inspect
def constructor(__init__):
@functools.wraps(__init__)
def new_init(self, *args, **kwds):
bound_args = inspect.signature(__init__).bind(*args, **kwds)
bound_args.apply_defaults()
for k, v in bound_args.arguments.items():
setattr(self, k, v)
return new_init
class Point:
@constructor
def __init__(x, y, z):
"""Constructs a Point object.
x - the x coordinate
y - the y coordinate
z - the z coordinate
"""
p = Point(1, 2, 3)
print(p.x, p.y, p.z) #=> 1 2 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment