simple symbolic math in Python

Rainer Deyke root at rainerdeyke.com
Sun Jan 7 22:41:46 EST 2001


"Kragen Sitaker" <kragen at dnaco.net> wrote in message
news:vE966.12210$K72.50241 at e420r-atl1.usenetserver.com...
> I posted most of this to kragen-hacks
> <kragen-hacks-subscribe at kragen.dnaco.net> late last millennium.
>
> I'm running into a problem: I can overload unary minus, but I can't
> overload e.g. math.sin and math.cos, because they're not methods.  It
> would be really nice to have a way to do that.  I can create objects
> that act just like dictionaries or files, including working with almost
> all of the built-in functions, but it doesn't look like I can create
> objects that look just like numbers.

import math

class MyNumber:
  ...

old_sin = math.sin

def my_sin(n):
  if isinstance(n, MyNumber):
    ...
  else:
    return old_sin(n)

math.sin = my_sin


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list