Skip to content

Instantly share code, notes, and snippets.

@milesrout
Created May 17, 2017 21:46
Show Gist options
  • Save milesrout/6f016f1058a93f5fb67d0aef8ae83bef to your computer and use it in GitHub Desktop.
Save milesrout/6f016f1058a93f5fb67d0aef8ae83bef to your computer and use it in GitHub Desktop.
from math import sqrt
class Dedent:
@classmethod
def method(cls, f=None, *, name=None):
def decorator(f):
setattr(cls, name if name is not None else f.__name__, f)
if f is not None:
decorator(f)
else:
return decorator
class Point(Dedent):
pass
@Point.method
def __init__(self, x, y):
self.x = x
self.y = y
@Point.method(name='__matmul__')
def dot(p, q):
return p.x * q.x + p.y * q.y
print(Point(1, 1) @ Point(1, -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment