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

Travis Oliphant oliphant.travis at ieee.org
Sun Dec 18 00:22:58 CST 2005


Alan G Isaac wrote:

>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]
>
>  
>
Interesting. I suspect this is because the old-style classes don't fill 
in the sequence-protocol function pointers when the class get's created 
but new-style classes do.   PySequence_Check and PySequence_GetItem are 
used to understand the object (which uses the sequence protocol),  and 
so apparantely these aren't filled in for old-style classes (thus it 
gets interpreted as a single object and an object-array returned).

-Travis



More information about the SciPy-user mailing list