News for hackers. Stuff that matters.
Re:Dear Python by stcredzero on Tuesday February 02, 2010 @04:56PM (#1096277)
It all started with your lists. I think that was when I really fell in love. You see in a language like Java or php if you want to get the last element of the list you have to write something like $a[count($a) - 1]. But python was different: just write a[-1].

Don't take this as hostile. Rather as an observation of the shifted expectations of programmer subculture.

So a cryptic negative index counts as "elegant?" Wouldn't it be more straightforward just to ask a list for "last?"

Here's the equivalent Smalltalk:

     list last
In Ruby:

     list.1ast
Laypeople would have an immediate inkling of either. The negative array index stuff would cause at least some python prompt exploration or the shift to some sort of reference.
Re:Dear Python by viraptor on Tuesday February 02, 2010 @04:58PM (#1096289)
That's clear and elegant for the last element. But now you need another function for [-2].
Re:Dear Python by jamesbritt on Tuesday February 02, 2010 @07:27PM (#1096684)
So ... what? Don't have clear, intuitive methods for common cases?

Ruby also provides arr[-1], arr[-2], etc. But there's a subtle difference when I read arr.last vs. arr[-1].

Re:Dear Python by codexon on Tuesday February 02, 2010 @08:52PM (#1096834)
The first index is 0, the next is 1, and if you loop around backwards, it's -1.

Makes sense to me.

Re:Dear Python by jamesbritt on Wednesday February 03, 2010 @01:11AM (#1097191)
"Makes sense to me."

Me too.

But: when I see code that refers to first and last, I expect that the first and last elements are special.

When I see integers, it leads me to think that all of the elements are about equally important.

I also find it easier to read the text form rather than the integers; I don't have to look carefully to see if it's 1, 11, 7; code reading goes more smoothly.

Having both forms allows me to express, and parse, intention better.

Re:Dear Python by stcredzero on Tuesday February 02, 2010 @09:13PM (#1096866)
That's another demonstration of my point. Many, many people out there in the non-tech world would expect that the first element would have an index of "1". First = "1st" = "#1." On the other hand, we often experience the convenience of pointer arithmetic (or similar) with zero-indexed arrays.

Yet another programmer/techy and ordinary person divide.

Re:Dear Python by codexon on Tuesday February 02, 2010 @09:29PM (#1096902)
If you want something for non-techies, you pick something like Applescript.

0-based indexing is noob friendly because most cs algorithms use it so you can do a straightforward translation. Would it be better to go with 1-based and make this harder for novice programmers? I don't think so.

Re:Dear Python by llimllib on Tuesday February 02, 2010 @05:08PM (#1096312)
Maybe by analogy to cddr, ruby should have laast?
Re:Dear Python by mnemonik on Tuesday February 02, 2010 @09:48PM (#1096931)
But then you are throwing all the readability gains of list.last vs. list[-1] right out the window and we are back to square one.

I don't think anyone finds Lisp's car and cdr (much less cadr and friends) particularly intuitive when they are first introduced to whichever dialect it happens to be.

You get used to it, and when needed, write specific-case functions for readability that are simple wrappers to those combo functions. Which I think is probably best no matter whether you have last or [-1], this argument is kind of pointless. Step back and think about why you need that item, write a small wrapper function to grab that it and name the wrapper well so it provides a little semantic context to whoever ends up reading the code later.

Re:Dear Python by llimllib on Tuesday February 02, 2010 @10:43PM (#1097000)
> But then you are throwing all the readability gains of list.last vs. list[-1] right out the window and we are back to square one.

ermmm, I apparently forgot to add the <sarcasm> tag.