Negative array indicies and slice()

Andrew Robinson andrew3 at r3dsolutions.com
Mon Oct 29 11:42:39 EDT 2012


On 10/29/2012 10:09 AM, Ian Kelly wrote:
> 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?
There's already a version of the python interpreter which fits in under 
100K:
http://code.google.com/p/python-on-a-chip/
It's not the 3.x series, though; and I don't want to redo this once 2.7 
really does become obsolete.
>> 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.
That's what I am curious about.
What unexpected behaviors would a "monkey patch" typically cause?
If no one really uses negative and positive indexes in the same slice 
operation, because there is no reason to do so...
It will only break the occasional esoteric application.

>
> 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.
>
>

No, there was no error at all.  Pthon just crashed & exited; not even an 
exception that I can recall.   It was if it exited normally!

The list was generated in a single pass by many .append() 's, and then 
copied once -- the original was left in place; and then I attempted to 
slice it.

I am able to routinely to 5 million length lists, copy, slice, cut, 
append, and delete from them without this ever happening.
If fragmentation were the issue, I'd think the shorter lists would cause 
the problem after many manipulations...

It may not be a bug in python itself, though, of course.  There are 
libraries it uses which might have a bug.




More information about the Python-list mailing list