Skip to content

Instantly share code, notes, and snippets.

@mitaki28
Created December 2, 2012 17:40
Show Gist options
  • Save mitaki28/4190098 to your computer and use it in GitHub Desktop.
Save mitaki28/4190098 to your computer and use it in GitHub Desktop.
paren to factor
# ~*~ coding:utf-8 ~*~
"""
数学でよく使う、掛け算記号の省略をやってみた
"""
def _(method):
def __(self, *args, **kw):
r = getattr(float, method)(self, *args, **kw)
return Float2(r) if isinstance(r, float) else r
return __
def _meta(cls, basetypes, d):
methods = dir(float)
methods = (method for method in methods if method not in d)
for method in methods:
if method not in ['__new__', '__init__',
'__getattr__', '__getattribute__',
'__setattr__', '__setattribute__',
'__delattr__']:
d[method] = _(method)
return type(cls, basetypes, d)
class Float2(float):
__metaclass__ = _meta
def __call__(self, x):
return Float2(self * x)
if __name__ == '__main__':
x = Float2(1.0)
y = Float2(1.0)
print (x + 3)(x - 2)
print x(x + 1)(x + 2)(x + 3)
print (x + 1)(y - 2) + (x + 1)(y + 3) == (x + 1)(2 * y + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment