Negative array indicies and slice()

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 29 13:09:01 EDT 2012


On Oct 29, 2012 7:10 AM, "Andrew Robinson" <andrew3 at r3dsolutions.com> wrote:
> I will be porting Python 3.xx to a super low power embedded processor (MSP430), both space and speed are at a premium.
> Running Python on top of Java would be a *SERIOUS* mistake.  .NET won't even run on this system. etc.

If that's the case, then running Python at all is probably a mistake.
You know the interpreter alone has an overhead of nearly 6 MB?

> Yes, I realize that.
> But, why can't I just overload the existing __getitem__ for lists and not bother writing an entire class?

You can just overload that one method in a subclass of list.  Being
able to monkey-patch __getitem__ for the list class itself would not
be advisable, as it would affect all list slicing anywhere in your
program and possibly lead to some unexpected behaviors.

> Hmmm..
> Let's try your example exactly as shown...
>
> "hello world"[aslice]
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'aslice' is not defined
>
> WOW. Cool.
> Where did the blanks *actually* get filled in?  Or HOW WILL they in your next post?

It appears that Steven omitted the definition of aslice by mistake.
It looks like it should have been:

aslice = slice(4, None, 2)

>  Looking at some of the online programming notes -- a slice apparently doesn't use an integer storage variable that is capable of arbitrary expansion. =-O -- and hence, won't work for very large sized lists.  That actually explains some crashes I have noted in the past when working with 20 million element lists that I wanted a slice of.  I had *plenty* of ram on that system.

20 million is nothing.  On a 32-bit system, sys.maxsize == 2 ** 31 -
1.  If the error you were seeing was MemoryError, then more likely you
were running into dynamic allocation issues due to fragmentation of
virtual memory.



More information about the Python-list mailing list