Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Hack request: rational numbers

0 views
Skip to first unread message

Anton Mellit

unread,
Oct 26, 2007, 4:02:01 PM10/26/07
to Pytho...@python.org
> Hi!
>
> I am using rational numbers from the gmpy module and I find that creating
> one in Python using
>
>>>> mpq(3,4)
>
> is rather clumsy. Clearly, minimal representation of this rational number
> is
>
> 3/4
>
> but in Python this expression has different meaning.

Hi,
I am writing an interface module with the PARI library and I had the
same question. So I implemented division operator for my new type
which of course did not work if both division arguments are integers.
Say 3/4 gives zero. But I discovered very simple hack. I add the
following string to my module initialization function:

PyInt_Type.tp_as_number->nb_divide = gen_div;

Here gen_div is the function I use for division of objects of my new type.

This solution is pretty nice since I do not have to make changes into
python or interactive shell. It is even better to make a function
which would 'turn on/off' my division. It is strange that it is only
possible to do such things in C code, I cannot do it from python.

Anton

0 new messages