module confict? gmpy and operator

Tim Peters tim.peters at gmail.com
Mon May 22 23:19:06 EDT 2006


[mensanator at aol.com]
> ##    Holy Mother of Pearl!
> ##
> ##    >>> for i in range(10):
> ##            for j in range(10):
> ##                    print '%4d' % (gmpy.mpz(i)*gmpy.mpz(j)),
> ##            print
> ##
> ##
> ##       0    0    0    0    0    0    0    0    0    0
> ##       0    1    2    3    1    5    6    7    2    9
> ##       0    2    4    6    2   10   12   14    4   18
> ##       0    3    6    9    3   15   18   21    6   27
> ##       0    1    2    3    1    5    6    7    2    9
> ##       0    5   10   15    5   25   30   35   10   45
> ##       0    6   12   18    6   30   36   42   12   54
> ##       0    7   14   21    7   35   42   49   14   63
> ##       0    2    4    6    2   10   12   14    4   18
> ##       0    9   18   27    9   45   54   63   18   81
>
> No wonder I couldn't figure out why my program wasn't working,
> gmpy mutiplication seems to have stopped working.
>
> My guess is that when I did
>
>     import gmpy
>     import random
>     import operator
>
> I somehow messed up gmpy's mutilpication. I brought in operator
> because I needed to do
>
>     NN = sum(map(operator.__mul__,X,C))
>
> which seemed to work ok. It was later when I got
>
>     8 * 3 = 6        (the 3 was an mpz)
>
> that I got in trouble. So I got rid of operator and did
>
>     def product(a,b): return a*b
>
>     NN = sum(map(product,X,C))
>
> which seems to work ok.

Please try to give a complete program that illustrates the problem.
For example, this program shows no problem on my box (Windows Python
2.4.3):

"""
import gmpy
import random
import operator

NN = sum(map(operator.__mul__, [1, 2, 3], [4, 5, 6]))

for i in range(10):
    for j in range(10):
        print '%4d' % (gmpy.mpz(i)*gmpy.mpz(j)),
    print
"""

The output was:

   0    0    0    0    0    0    0    0    0    0
   0    1    2    3    4    5    6    7    8    9
   0    2    4    6    8   10   12   14   16   18
   0    3    6    9   12   15   18   21   24   27
   0    4    8   12   16   20   24   28   32   36
   0    5   10   15   20   25   30   35   40   45
   0    6   12   18   24   30   36   42   48   54
   0    7   14   21   28   35   42   49   56   63
   0    8   16   24   32   40   48   56   64   72
   0    9   18   27   36   45   54   63   72   81

> Am I correct that there is a conflict with
>
>     import gmpy
>     import operator

I don't see a problem of any kind, and there's no way to guess from
the above what you did that mattered.  It's hard to believe that
merely importing any module (operator or otherwise) has any effect on
gmpy.

> Would reversing the import order help or should I just avoid
> mixing them?

Both are dubious.



More information about the Python-list mailing list