Created
September 5, 2016 11:21
-
-
Save milesrout/dc034232e03c1753a80fe9954adcbd0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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