Dunham
Dunham

Reputation: 123

python 3, difference in two tuple constructions

I have a script for doing some math computations, where I am using dictionaries to represent polynomials. Originally my code started off as this:

p = {}
p[(0,0,0)] = 1

Then, to generalize, I replaced the second command by this:

p[tuple(numpy.zeros((3,),dtype=int))] = 1

What I do not understand is why this single change affects the outcome of the program. How are these different?

Upvotes: 0

Views: 79

Answers (1)

Chris_Rands
Chris_Rands

Reputation: 41168

As I said in my comment, the difference is the numpy version creates a tuple of numpy.int64s instead of regular Python ints:

>>> import numpy
>>> t1 = (0,0,0)
>>> type(t1[0])
<class 'int'>
>>> t2 = tuple(numpy.zeros((3,),dtype=int))
>>> type(t2[0])
<class 'numpy.int64'>

This is unlikely to make a difference but can in a few cases, for example with some itertools arguments. There is a related bug issue on this too, so watch this space.

Upvotes: 1

Related Questions