Daniel Robert-Nicoud Daniel Robert-Nicoud - 1 year ago 43
Python Question

Python automatically converting data types

I have implemented a class

, containing also a method
to be able to print stuff. I also have a function
converting certain types of strings to BasicForm objects.

Later in my program, I have an array
of allowed strings, and I do the following:

for i in xrange(len(array)):
array[i] = stringToBasicForm(array[i])

My problem is that, after this,
appears to contain objects of type
and not of type
as I would have expected. I think that Python is automatically converting my BasicForms to strings using
for some reason.

What is happening? How can I make it so that my array contains the good type of objects at the end (without creating an auxiliary array)?

Any help will be greatly appreciated.

Remark: I'm using Python 2.7

Here's a small-ish working example:

from fractions import *
from numpy import *

class BasicForm:

def __init__(self,n,coeff,I):

if shape(I)[1] != n + 1:
print "Error: illegal I."
if not all([isinstance(f,Fraction) for f in coeff]):
print "Error: coefficients must be of class Fraction."

self.n = n

vect = zeros(2**(n+1)-1,dtype = Fraction)
for i in xrange(len(I)):
if not any(I[i]):
print "Error: the empty set doesn't code any basic form."
vect[subsetToIndex(n,I[i])] = coeff[i]
self.vect = vect

def __str__(self):

if not any(self.vect):
return "0"

s = ""
first = True
for i in xrange(2**(self.n+1)-1):
if self.vect[i] == 0:
if self.vect[i] < 0:
s += "-"
if not first:
s = s[:-3] + "- "
if self.vect[i] != 1:
s += str(abs(self.vect[i])) + "*"
s += "("
I = indexToSubset(self.n,i)
for k in xrange(self.n+1):
if I[k]:
s += str(k)
s += ") + "
first = False

return s[:-2]

def stringToBasicForm(n,string):

out = BasicForm(n,[Fraction(0)],[ones(n+1,dtype = bool)])

return out

Now for the weird part. If I run this with

n = 1
array = ["(01)"]
for i in xrange(len(array)):
array[i] = stringToBasicForm(n,array[i])
print isinstance(array[i],BasicForm), isinstance(array[i],str)

everything works as expected (output:
True False
). But if I run it with

def opPBT(n,LPBT,BF_array):

tree = copy(LPBT)
array = copy(BF_array)

# Convert basic forms to elements to the Sullivan algebra (injection).
for i in xrange(len(array)):
if isinstance(array[i],str):
array[i] = stringToBasicForm(n,array[i])
print isinstance(array[i],BasicForm), isinstance(array[i],str)
elif array[i].n != n:
print "Error: basic form of wrong dimension."
array[i] = basicToSullivan(array[i])

n = 2
forms = ["(01)"]
lcomb = []

which is what I would like to do, then the output is
False True
, and I have absolutely no clue of what I'm doing wrong (it's probably some stupid error but I'll be damned if I can see it...)

val val
Answer Source

Numpy arrays are strongly typed:

>>> x = np.empty((3,), dtype=np.float64)
>>> x[0] = 1  # I put an int in...
>>> x[0]      # And get a float out !

Usually, the array will try to convert values you pass in (which is typically what you want, cf the example above, where you wouldn't want the whole thing to crash because you forgot a . after the 1). Since you have a convenient __str__, it will use that to ensure that your array is consistent.

What does that have to do with your problem ? I suspect the copy you're using is the numpy copy:

>>> from numpy import copy
>>> copy([1,2,3]) # List in, array out...
array([1, 2, 3])

Which would explain your automatic coercion; normal Python lists do not operate like that:

>>> foo = ['a', 'b', 'c']
>>> class A(object):
...     def __str__(self):
...         return "converted !"
>>> bar = copy(foo)
>>> bar[0] = A()
>>> foo[0] = A()
>>> type(foo[0])
>>> type(bar[0])
>>> isinstance(bar[0], str)