[SciPy-user] sequence emulation -/-> array access?

Alan G Isaac aisaac at american.edu
Sat Dec 17 19:49:18 CST 2005


On Sat, 17 Dec 2005, Travis Oliphant apparently wrote: 
> class myobj(object): 

Argh!
So the only issue was that it needs to be a "New Class" 
instead of a "Classic Class"!

Thanks!!
Alan

PS Illustrative details:

################  script  #######################
import scipy as S

class new(object):
    def __init__(self, data):
        self.data = data
    def __len__(self):
        return len(self.data)
    def __getitem__(self, key):
        return self.data[key]

class old:
    def __init__(self, data):
        self.data = data
    def __len__(self):
        return len(self.data)
    def __getitem__(self, key):
        return self.data[key]

a1 = new([1,2,3,4])
a2 = old([1,2,3,4])
b1 = S.array(a1)
print b1[0], "new class works fine"
b2 = S.array(a2)
print "but old class does not", b2[0]


################  output  #######################
1 new class works fine
but old class does not
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:/temp5.py", line 24, in ?
    print "but old class does not", b2[0]
ValueError: 0-d arrays can't be indexed.





More information about the SciPy-user mailing list