fork download
  1. import cmath
  2. import math as rmath
  3. from types import *
  4.  
  5. class MathSelector(object):
  6. def __init__(self, fn):
  7. self.fn = fn
  8. def __call__(self,*args):
  9. try:
  10. return getattr(rmath,self.fn)(*args)
  11. except:
  12. return getattr(cmath,self.fn)(*args)
  13.  
  14. class Math(object):
  15. def __getattr__(self, fn):
  16. return MathSelector(fn)
  17.  
  18. math = Math()
  19.  
  20. print math.sqrt(-1)
  21. print math.exp(complex(0,1))
  22. print math.sin(2.5)
Success #stdin #stdout 0.02s 9016KB
stdin
Standard input is empty
stdout
1j
(0.540302305868+0.841470984808j)
0.598472144104