[pypy-dev] Basic math ops

Benjamin Peterson benjamin at python.org
Tue Mar 8 18:14:27 CET 2011

2011/3/8 Timothy Baldridge <tbaldridge at gmail.com>:
> In my interpreter, I have several primitive wrapper objects:
> IntObject, FloatObject, etc.
> Currently I'm planning to create functions for every combination:
> def add_int_float(i, f):
>     return FloatObject(i.int_value() + f.float_value())
> then I'd need some sort of dispatch code that would say
> if isinstance(arg1, IntObject) and isinstance(arg2, FloatObject):
>    return add_int_float(arg1, arg2)
> It seems like pypy should have something to do this already. I saw
> some information on this but that seemed specific to the Python
> interpreter.

You could use multimethods. Look at the stuff in pypy/objspace/std/
for examples. (It's a bit messy.)

> What's the recommended procedure on this? Is there a way to tell pypy
> "this object wraps a single value, so keep it unwrapped if you can",
> or does pypy figure that out automagically?

The JIT will unwrap things automatically.


More information about the Pypy-dev mailing list