Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5247

Re: Slice implementation bug

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:bug': 0.04; 'value,': 0.04; 'default,': 0.07; 'slice': 0.07; 'something,': 0.07; 'foo': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'underlying': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'anymore.': 0.16; 'boring': 0.16; 'enigma': 0.16; 'generator.': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'kern': 0.16; 'received:216.62': 0.16; 'received:216.62.213': 0.16; 'received:enthought.com': 0.16; 'tuple):': 0.16; 'you)': 0.16; 'case.': 0.16; 'interpret': 0.19; 'yield': 0.19; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'skip:_ 20': 0.24; 'define': 0.26; 'function': 0.27; "doesn't": 0.28; 'elements': 0.29; 'fine.': 0.29; 'work:': 0.29; 'implement': 0.30; 'seem': 0.30; 'to:addr:python-list': 0.32; 'another': 0.32; 'using': 0.34; 'header:X-Complaints-To:1': 0.34; 'actually': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'it?': 0.37; 'element': 0.38; 'but': 0.38; 'received:org': 0.38; 'though': 0.38; 'hello!': 0.39; 'skip:s 30': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'attempt': 0.40; 'would': 0.40; 'header:Received:5': 0.40; 'our': 0.63; 'world': 0.65; 'believe': 0.66; 'eco': 0.84; 'matrix': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: Slice implementation bug
Date Thu, 12 May 2011 12:24:11 -0500
Organization The Church of Last Thursday
References <BANLkTikKg0o0zAAmN9miqfr_Dto1qc6hoA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host outbound.enthought.com
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
In-Reply-To <BANLkTikKg0o0zAAmN9miqfr_Dto1qc6hoA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1480.1305221070.9059.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 82.94.164.166
X-Trace 1305221070 news.xs4all.nl 81484 [::ffff:82.94.164.166]:55739
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5247

Show key headers only | View raw


On 5/12/11 6:06 AM, Tambet wrote:
> Hello!
>
> Let's say slice is multidimensional now - how to interpret it?
>
> I excpect these to work:
>
>     * m[0, 3] - get one element from matrix
>     * m[0:2, 0:2] - get four elements from matrix, iterate over them (I have
>       actually an rtree if it doesn't make sense to you)
>
> But it won't, because if m[0, 3] returns something, then m[0:2, 0:2] cannot
> yield anymore.

Let me try to rephrase, since you seem to be leaving out a lot of assumptions. 
You are trying to say that you want m[0:2,0:2] to return an iterator and that if 
you define __getitem__() such that m[0,3] returns a value, then you cannot 
implement __getitem__() to be a generator using a yield statement.

Okay. Fine.

But there is no reason that __getitem__() needs to be a generator function for 
you to return an iterator. You can simply return another generator. For example:

def __getitem__(self, key):
     if isinstance(key, tuple):
         if any(isinstance(x, slice) for x in key):
             return self._generate_from_slice(key)
     # The default, boring case.
     return self._get_value(key)

def _generate_from_slice(self, key):
     yield foo
     yield bar
     yield etc

> Ofcourse I could return an iterator, but this would not be so simple.

Really, it is.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Slice implementation bug Robert Kern <robert.kern@gmail.com> - 2011-05-12 12:24 -0500

csiph-web