[IPython-user] Macros & slice notation

Fernando Perez Fernando.Perez at colorado.edu
Fri Dec 30 12:02:41 CST 2005

Ville Vainio wrote:
> Now that I took the while to look into macros, I wondered what is the
> rationale for using the python slice notation (i.e. 5:7 takes lines
> 5,6 and not including 7). It seems a bit counter-intuitive considering
> that it always deals with line numbers, and once you do "hist" and see
> the line numbers, you *are* going to think 5:7 means "take lines
> 5,6,7". I understand that the python slice notation has its advantages
> (esp. when the step != 1) but for line numbers it seems a bit
> unpractical. With line numbers the users won't be thinking of
> intervals between lines.
> It's not a major issue, really, but I'd expect it to be a minor gotcha
> for many users. I understand the argument of consistency with the rest
> of Python, of course.

Yes, it really was a matter of consistency only.   In fact, the original 
implementation for %save and %macro used N:M to mean N..M.  I decided to 
revert to the normal case, thinking of (import this):

Special cases aren't special enough to break the rules.

Though the next line says:

Although practicality beats purity.

This is a good case of tug of war between those two, and I can see the 
argument going both ways.  Even I find myself having to do the off-by-one 
change in my mind when I make a macro, so I could be swayed either way.

Opinions from others?  After all, if everyone thinks (as I originally did and 
Ville asks for) that in this case, N:M should mean N..M against Python's 
slicing rules, I'd be willing to make that change again.

Here's an idea: we could change the range syntax in %macro and %save to use 
N-M instead of N:M.  This would clearly indicate that this is NOT a regular 
python slice, and in that case, using N-M==N...M seems pretty reasonable to me.




More information about the IPython-user mailing list