Quantcast

overriding __getitem__

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

overriding __getitem__

Michael Hearne-2
In numpy, it is possible to use the square bracket operators [] in various ways:

x = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
x[0,0]
x[0:2,0:2]
x[0:4:2,0:4:2]

I presume that under the hood, the numpy array object is overriding the __getitem__ method.

Based on that assumption, I created the following test code that I _thought_ should work:
--------------------------------------------------------------------------------------------
import numpy
class Sequence:
    def __init__(self):        self.data = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
    def __getitem__(self,key1,key2):        if isinstance(key1,int) and isinstance(key2,int):            return(self.data[key1,key2])
if __name__ == "__main__":    s = Sequence()    s[2,2]--------------------------------------------------------------------------------------------However, I get the error:"TypeError: __getitem__() takes exactly 3 arguments (2 given)"
Isn't self an implied argument since __getitem__ is a class method?
Is this the wrong way to attain the interface I want?
Thanks,
Mike



------------------------------------------------------
Michael Hearne
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------



_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: overriding __getitem__

Matthieu Brucher-2
Hi,

In fact the __getitem__() method has only two parameters, as the second parameter is the list (or tuple ?) of the keys you have.

Matthieu

2008/4/8, Michael Hearne <[hidden email]>:
In numpy, it is possible to use the square bracket operators [] in various ways:

x = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
x[0,0]
x[0:2,0:2]
x[0:4:2,0:4:2]

I presume that under the hood, the numpy array object is overriding the __getitem__ method.

Based on that assumption, I created the following test code that I _thought_ should work:
--------------------------------------------------------------------------------------------
import numpy
class Sequence:
    def __init__(self):        self.data = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
    def __getitem__(self,key1,key2):        if isinstance(key1,int) and isinstance(key2,int):            return(self.data[key1,key2])
if __name__ == "__main__":    s = Sequence()    s[2,2]--------------------------------------------------------------------------------------------However, I get the error:"TypeError: __getitem__() takes exactly 3 arguments (2 given)"
Isn't self an implied argument since __getitem__ is a class method?
Is this the wrong way to attain the interface I want?
Thanks,
Mike



------------------------------------------------------
Michael Hearne
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------



_______________________________________________
SciPy-user mailing list
[hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://projects.scipy.org/mailman/listinfo/scipy-user" target="_blank">http://projects.scipy.org/mailman/listinfo/scipy-user




--
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: overriding __getitem__

Stefan Seefeld
In reply to this post by Michael Hearne-2
Michael Hearne wrote:

> --------------------------------------------------------------------------------------------
> import numpy
> class Sequence:
>     def __init__(self):        self.data =
> numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
>     def __getitem__(self,key1,key2):        if isinstance(key1,int) and
> isinstance(key2,int):            return(self.data[key1,key2])
> if __name__ == "__main__":    s = Sequence()  
>  s[2,2]--------------------------------------------------------------------------------------------However,
> I get the error:"TypeError: __getitem__() takes exactly 3 arguments (2
> given)"
> Isn't self an implied argument since __getitem__ is a class method?
> Is this the wrong way to attain the interface I want?


I did the following little experiment:

class Sequence:
     def __getitem__(self, *args): print 'getitem', args

s = Sequence()
s[1]
s[1,2]
s[1:2]
...

The above shows that 's[1,2]' will pass a single argument: a '(1,2)'
tuple, while 's[1:2,1:2]' will pass two arguments (two slice objects).
This is with python 2.5. A quick search didn't reveal any documentation
of this behavior.

This suggests that you may rewrite your definition of __getitem__ to be
more flexible in its expectations as to number and type of arguments.

(I'm not sure what makes you think __getitem__ is a class method. It's
an ordinary attribute, expecting the first argument to be the object
reference, just what you pass above.)

HTH,

                Stefan

--

       ...ich hab' noch einen Koffer in Berlin...
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: overriding __getitem__

Michael Hearne-2
Stefan and Matthieu - Thanks for the responses.  I will work on my code as you suggest.

Stefan - I think I used "class method" inappropriately - I think the appropriate term is "instance method".

--Mike 
On Apr 8, 2008, at 10:41 AM, Stefan Seefeld wrote:
Michael Hearne wrote:

--------------------------------------------------------------------------------------------
import numpy
class Sequence:
    def __init__(self):        self.data = 
numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
    def __getitem__(self,key1,key2):        if isinstance(key1,int) and 
isinstance(key2,int):            return(self.data[key1,key2])
if __name__ == "__main__":    s = Sequence()   
 s[2,2]--------------------------------------------------------------------------------------------However, 
I get the error:"TypeError: __getitem__() takes exactly 3 arguments (2 
given)"
Isn't self an implied argument since __getitem__ is a class method?
Is this the wrong way to attain the interface I want?


I did the following little experiment:

class Sequence:
     def __getitem__(self, *args): print 'getitem', args

s = Sequence()
s[1]
s[1,2]
s[1:2]
...

The above shows that 's[1,2]' will pass a single argument: a '(1,2)' 
tuple, while 's[1:2,1:2]' will pass two arguments (two slice objects).
This is with python 2.5. A quick search didn't reveal any documentation 
of this behavior.

This suggests that you may rewrite your definition of __getitem__ to be 
more flexible in its expectations as to number and type of arguments.

(I'm not sure what makes you think __getitem__ is a class method. It's 
an ordinary attribute, expecting the first argument to be the object 
reference, just what you pass above.)

HTH,

Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...
_______________________________________________
SciPy-user mailing list




------------------------------------------------------
Michael Hearne
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------



_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Loading...