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


Groups > comp.lang.python > #32577

Re: Negative array indicies and slice()

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andrew3@r3dsolutions.com>
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; 'python,': 0.02; 'heavily': 0.04; 'float': 0.05; 'modify': 0.05; 'none)': 0.07; 'parser': 0.07; 'pretend': 0.07; 'rejected': 0.07; 'type,': 0.07; 'api': 0.09; 'python': 0.09; '(1,': 0.09; '2006.': 0.09; 'integers': 0.09; 'itself,': 0.09; 'logic': 0.09; 'mutable': 0.09; 'pep': 0.09; 'subject:()': 0.09; 'unexpected': 0.09; 'bug': 0.10; 'def': 0.10; 'itself.': 0.11; "wouldn't": 0.11; 'index': 0.13; 'ignore': 0.13; '*can*': 0.16; 'assumptions': 0.16; 'both.': 0.16; 'computes': 0.16; 'container,': 0.16; 'down...': 0.16; 'exported': 0.16; 'hmmmm....': 0.16; 'hypothetical': 0.16; 'lambda': 0.16; 'merely': 0.16; 'numpy': 0.16; 'patch,': 0.16; 'report?': 0.16; 'sequence.': 0.16; 'subclassing': 0.16; 'subject:array': 0.16; 'syntactical': 0.16; 'wrote:': 0.17; 'implementing': 0.17; 'instance': 0.17; 'passes': 0.17; 'thu,': 0.17; 'tim': 0.18; '>>>': 0.18; 'memory': 0.18; '(or': 0.18; 'respective': 0.20; 'questions:': 0.22; 'simpler': 0.22; "user's": 0.22; 'skip:_ 20': 0.22; "i'd": 0.22; 'this:': 0.23; 'patch': 0.24; 'idea': 0.24; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'looks': 0.26; 'creating': 0.26; 'values': 0.26; 'am,': 0.27; 'implemented': 0.27; 'raw': 0.27; 'andrew': 0.27; 'container': 0.29; "d'aprano": 0.29; 'shoot': 0.29; 'use?': 0.29; 'yes.': 0.29; 'case,': 0.29; 'handled': 0.29; 'objects': 0.29; 'summary': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'usually': 0.30; 'becomes': 0.30; 'basic': 0.30; 'function': 0.30; 'code': 0.31; 'point': 0.31; 'asking': 0.32; 'implement': 0.32; 'could': 0.32; 'skip:s 30': 0.33; '2006': 0.33; 'interaction': 0.33; 'legacy': 0.33; 'to:addr:python-list': 0.33; 'knowledge': 0.33; 'that,': 0.34; 'version': 0.34; "can't": 0.34; 'list': 0.35; 'ahead': 0.35; 'filter': 0.35; 'moved': 0.35; 'nov': 0.35; 'sequence': 0.35; 'so,': 0.35; 'pm,': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'wanted': 0.36; "didn't": 0.36; 'method': 0.36; 'anything': 0.36; 'thank': 0.36; 'does': 0.37; 'why': 0.37; 'passed': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'comment': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'apply': 0.39; 'release': 0.39; 'takes': 0.39; 'is.': 0.62; 'received:phx3.secureserver.net': 0.62; 'received:prod.phx3.secureserver.net': 0.62; 'between': 0.63; 'information': 0.63; 'behavior': 0.64; 'header:Reply-To:1': 0.68; 'direct': 0.69; 'touch': 0.69; 'reply-to:no real name:2**0': 0.72; 'hand': 0.82; '(once': 0.84; '2.5.': 0.84; 'happened.': 0.84; 'overhead,': 0.84; 'pain': 0.84; 'touched': 0.84; 'conversation': 0.91; 'ethan': 0.91; 'received:173.201': 0.91
Date Thu, 01 Nov 2012 15:25:51 -0700
From Andrew Robinson <andrew3@r3dsolutions.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111126 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Negative array indicies and slice()
References <509053F2.6020900@r3dsolutions.com> <CALwzidnQ2bUdMp8c0xNomabcLHZBBtr_DYSSzvhz3jqeYNkWkQ@mail.gmail.com> <50912ADC.2020401@r3dsolutions.com> <CALwzid=_1TCQC5JryemVfVpBLWq=qZwy4hRjCPA5ha0vSm3=VA@mail.gmail.com> <50918716.3080305@r3dsolutions.com> <5092833F.4070609@stoneleaf.us> <50925DE6.7020100@r3dsolutions.com> <CALwzidkf6yaPz3C9qUtsUe-a+ojmpJNY0FxhBiykZmm6VssYTQ@mail.gmail.com>
In-Reply-To <CALwzidkf6yaPz3C9qUtsUe-a+ojmpJNY0FxhBiykZmm6VssYTQ@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To andrew3@r3dsolutions.com
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.3170.1351808865.27098.python-list@python.org> (permalink)
Lines 104
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351808865 news.xs4all.nl 6939 [2001:888:2000:d::a6]:41067
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32577

Show key headers only | View raw


On 11/01/2012 12:07 PM, Ian Kelly wrote:
> On Thu, Nov 1, 2012 at 5:32 AM, Andrew Robinson
> <andrew3@r3dsolutions.com>  wrote:
>> Hmmmm.... was that PEP the active state of Python, when Tim rejected the bug report?
> Yes. The PEP was accepted and committed in March 2006 for release in
> Python 2.5.  The bug report is from June 2006 has a version
> classification of Python 2.5, although 2.5 was not actually released
> until September 2006.
That explain's Peter's remark.  Thank you.  He looks *much* smarter now.

>
>> Pep 357 merely added cruft with index(), but really solved nothing.  Everything index() does could be implemented in __getitem__ and usually is.
> No.  There is a significant difference between implementing this on
> the container versus implementing it on the indexes.  Ethan
> implemented his string-based slicing on the container, because the
> behavior he wanted was specific to the container type, not the index
> type.  Custom index types like numpy integers on the other hand
> implement __index__ on the index type, because they apply to all
> sequences, not specific containers.

Hmmm...
D'Aprano didn't like the monkey patch;and sub-classing was his fix-all.

Part of my summary is based on that conversation with him,and you 
touched on one of the unfinished  points; I responded to him that I 
thought __getitem__ was under-developed.   The object slice() has no 
knowledge of the size of the sequence; nor can it get that size on it's 
own, but must passively wait for it to be given to it.

The bottom line is:  __getitem__ must always *PASS* len( seq ) to 
slice() each *time* the slice() object is-used.  Since this is the case, 
it would have been better to have list, itself, have a default member 
which takes the raw slice indicies and does the conversion itself.  The 
size would not need to be duplicated or passed -- memory savings, & 
speed savings...

I'm just clay pidgeoning an idea out here....
Let's apply D'Aprano 's logic to numpy; Numpy could just have subclassed 
*list*; so let's ignore pure python as a reason to do anything on the 
behalf on Numpy:

Then, lets' consider all thrid party classes;  These are where 
subclassing becomes a pain -- BUT: I think those could all have been 
injected.

 >>> class ThirdParty( list ):  # Pretend this is someone else's...
...     def __init__(self): return
...     def __getitem__(self,aSlice): return aSlice
...

We know it will default work like this:
 >>> a=ThirdParty()
 >>> a[1:2]
slice(1, 2, None)

# So, here's an injection...
 >>> ThirdParty.superOnlyOfNumpy__getitem__ = MyClass.__getitem__
 >>> ThirdParty.__getitem__ = lambda self,aSlice: ( 1, 3, 
self.superOnlyOfNumpy__getitem__(aSlice ).step )
 >>> a[5:6]
(1, 3, None)

Numpy could have exported a (workable) function that would modify other 
list functions to affect ONLY numpy data types (eg: a filter).  This 
allows user's creating their own classes to inject them with Numpy's 
filter only when they desire;

Recall Tim Peter's "explicit is better than implicit" Zen?

Most importantly normal programs not using Numpy wouldn't have had to 
carry around an extra API check for index() *every* single time the 
heavily used [::] happened.  Memory & speed both.

It's also a monkey patch, in that index() allows *conflicting* 
assumptions in violation of the unexpected monkey patch interaction worry.

eg: Numpy *CAN* release an index() function on their floats -- at which 
point a basic no touch class (list itself) will now accept float as an 
index in direct contradiction of PEP 357's comment on floats... see?

My point isn't that this particular implementation I have shown is the 
best (or even really safe, I'd have to think about that for a while).  
Go ahead and shoot it down...

My point is that, the methods found in slice(), and index() now have 
moved all the code regarding a sequence *out* of the object which has 
information on that sequence.  It smacks of legacy.

The Python parser takes values from many other syntactical constructions 
and passes them directly to their respective objects -- but in the case 
of list(), we have a complicated relationship; and not for any reason 
that can't be handled in a simpler way.

Don't consider the present API legacy for a moment, I'm asking 
hypothetical design questions:

How many users actually keep slice() around from every instance of [::] 
they use?
If it is rare, why create the slice() object in the first place and 
constantly be allocating and de-allocating memory, twice over? (once for 
the original, and once for the repetitive method which computes dynamic 
values?)  Would a single mutable have less overhead, since it is 
destroyed anyway?

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


Thread

Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-01 15:25 -0700
  Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-02 02:14 +0000
    Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-02 01:57 -0700
    Re: Negative array indicies and slice() Robert Kern <robert.kern@gmail.com> - 2012-11-02 10:48 +0000

csiph-web