I have implemented a class BasicForm, containing also a method __str__ to be able to print stuff. I also have a function stringToBasicForm(string) converting certain types of strings to BasicForm objects.
Later in my program, I have an array 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, array appears to contain objects of type str and not of type BasicForm as I would have expected. I think that Python is automatically converting my BasicForms to strings using __str__ 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."
return
if not all([isinstance(f,Fraction) for f in coeff]):
print "Error: coefficients must be of class Fraction."
return
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."
return
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:
continue
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."
return
array[i] = basicToSullivan(array[i])
return
n = 2
forms = ["(01)"]
lcomb = []
opPBT(n,lcomb,forms)
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...)
stringToBasicFormdo?stringToBasicFormreturns objects of typeBasicFormthe list will be filled with those, python does no conversion by itself.