Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'element': 0.07; 'indexing': 0.07; '[0,': 0.09; 'arguments': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'rows,': 0.09; '~ethan~': 0.09; 'python': 0.11; 'def': 0.12; '(1,': 0.16; '2.7.3': 0.16; 'brackets': 0.16; 'caveat': 0.16; 'ideally,': 0.16; 'none),': 0.16; 'wrote:': 0.18; "python's": 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'certain': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'subject:) ': 0.29; 'restrict': 0.30; "i'm": 0.30; 'consisting': 0.31; 'sep': 0.31; 'allows': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'test': 0.35; 'located': 0.36; 'library.': 0.36; 'object,': 0.36; 'charset:us-ascii': 0.36; 'example,': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'john': 0.61; 'received:173': 0.61; 'myself': 0.63; 'more': 0.64; 'between': 0.67; '4th': 0.74; 'square': 0.74; 'behavior': 0.77; 'column.': 0.84; 'received:gateway14.websitewelcome.com': 0.84 Date: Wed, 09 Apr 2014 13:34:06 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Method(s) called by square brackets, slice objects References: <74d6fb5c-baca-4606-9fa8-66238a561e0c@googlegroups.com> In-Reply-To: <74d6fb5c-baca-4606-9fa8-66238a561e0c@googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source-IP: 173.12.184.233 X-Exim-ID: 1WXzCp-00025n-KF X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:54978 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397077000 news.xs4all.nl 2940 [2001:888:2000:d::a6]:38951 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69983 On 04/09/2014 01:24 PM, John Ladasky wrote: > > I would like to build a multi-dimensional array that allows numpy-style > indexing and, ideally, uses Python's familiar square-bracket and slice > notations. > > For example, if I declare a two-dimensional array object, x, then x[4,7] > retrieves the element located at the 4th row and the 7th column. If I > ask for x[3:6,1:3], I get a 3 x 2 array object consisting of the inter- >section of the 3rd-5th rows, and the 1st-2nd columns, of x. > > In this case I'm not allowed to use numpy, I have to restrict myself to > the standard library. I thought that I might achieve the desired behavior > by defining an object with specific __getitem__ and/or __getslice__ methods. > However, the documentation of these methods that I am reading suggests that > the arguments are pre-parsed into certain formats which may not allow me to > do things numpy's way. Is this true? Nope. Whatever you put between the square brackets is what gets passed into __getitem__; the only caveat is that anything with : will be turned into a slice: Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. --> class GetIndex(object): ... def __getitem__(self, thing): ... print thing ... return None ... --> test = GetIndex() --> test[1] 1 --> test [1,2] (1, 2) --> test[1:3, 4:5] (slice(1, 3, None), slice(4, 5, None)) --> test[range(3)] [0, 1, 2] -- ~Ethan~