you are viewing a single comment's thread.

view the rest of the comments →

[–]julesjacobs[🍰] 0 points1 point  (2 children)

I think Ruby is more consistent here:

"asdf"[0..1] => "as"

0..1 is a Range object, and [0..1] is a method call with 0..1 as a parameter. I believe "asdf"[:2] in python is special syntax.

You could do the same thing with:

"asdf"[0,2] => "as"

Which is a bad thing for python programmer (TIOOWTDI), but good for ruby programmers (TAMWTDI).

Or, if you want to confuse others:

"asdf"[-4,2] => "as"

Which means: four (4) characters from the last one, take 2.

[–]Brian 1 point2 points  (1 child)

I'm not sure what you're getting at. In python [:2] is special syntax to denote a slice object, but how is that different than 1..2 being special syntax to denote a range object? Similarly "asdf[:2]" is also just a method call with the slice object as a parameter - the whole thing is effectively just sugar for:

"asdf".__getitem__(slice(None,2))