Possible improvement to slice opperations.

Steve Holden steve at holdenweb.com
Mon Sep 5 15:28:07 EDT 2005


Ron Adam wrote:
> Magnus Lycka wrote:
> 
> 
>>Ron Adam wrote:
>>
>>
>>>Slicing is one of the best features of Python in my opinion, but
>>>when you try to use negative index's and or negative step increments
>>>it can be tricky and lead to unexpected results.
>>
>>
>>Hm... Just as with positive indexes, you just need to understand
>>the concept properly.
>>
>>
>>>This topic has come up fairly often on comp.lang.python, and often 
>>>times, the responses include:
>>>
>>>    * Beginners should avoid negative extended slices.
>>>
>>>    * Slices with negative step increments are for advanced
>>>      python programmers.
>>
>>
>>I certainly wouldn't respond like that...
>>
>>
>>>    * It's not broke if you look at it in a different way.
>>>
>>>    * You should do it a different way.
>>
>>
>>Those are correct, but need to be complemented with helpful
>>explanations. It's often like this, that we think things seem
>>strange, because we don't see it right. We need a new perspective.
>>For instance, it took me a long time to understand OOP, but once
>>the concept was clear, things fell into place. Often, we fail to
>>see things due to preconceptions that mislead us. Like Yoda said:
>>"You need to unlearn what you have learnt."
> 
> 
> To me, seeing a lot responses of this sort is an indicator it needs to 
> be simplified/or that something isn't right about them.  They shouldn't 
> be that difficult to use or explain.
> 
It's a common misconception that all ideas should be explainable simply. 
This is not necessarily the case, of course. When a subject is difficult 
then all sorts of people bring their specific misconceptions to the 
topic, and suggest that if only a few changes were made the whole thing 
would be easier to understand.

Unfortunately, while that might make the topic in question easier to 
understand for some it would make it difficult, and even 
counter-intuitive, for others. As many have said before me, there's a 
reason why slicing and indexing are the way they are. The interfaces 
were designed by people with long years of programming and teaching 
experience.

[...]
You said it quite well yourself:
> 
> It's easy to think you understand something when you don't. I spend 
> quite a while figuring this out, And am sure about how it works. If 
> there are issues with this, then it will probably be in how I describe 
> it, what words or terminology is used, and weather or not it's the 
> proper approach.
> 
> There are actually two distinct proposals here, not just one.
> 
>      1. Fixing negative strides so they return the slice indexed as you 
> say they should.
> 
>      2. Using base one negative index's and picking item from the right 
> of negative index's instead of the right.
> 
> 
> They don't both need to implemented, Item 1 could be fixed in 2.5.
> 
Given that Python has a 1's-complement operator already I don;t see why 
you can't just leave Python alone and use it, since it seems to keep you 
happy. If "fixing" item 1 in 2.5 would induce major code breakage, 
there's less than a snowball's chance in hell it will happen.

"Professor Einstein, could you tell our readers how general relativity 
works?"

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list