classification
Title: Add list.get
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, gruszczy, rhettinger
Priority: normal Keywords: patch

Created on 2011-04-19 18:17 by gruszczy, last changed 2011-04-22 16:47 by eric.araujo. This issue is now closed.

Files
File name Uploaded Description Edit
list.get.patch gruszczy, 2011-04-19 18:17 review
Messages (5)
msg134087 - (view) Author: Filip GruszczyƄski (gruszczy) Date: 2011-04-19 18:17
I have proposed on Core Mentorship list to add get(index, default) method to list object. I was suggested to bring it up here and also told, that improving operator module could be a better solution. This is why I would like to ask, if it makes sense to work on this?

I attach a patch for list objects. I would post a link to Core Mentorship thread, but I believe the archive is private.

The original idea came from a question on StackOverflow: http://stackoverflow.com/questions/2492087/how-to-get-the-nth-element-of-a-python-list-or-a-default-if-not-available
msg134088 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-19 18:20
Sorry, this has come up before and was rejected.
msg134090 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-19 19:10
What did you have in mind for the operator module?
msg134092 - (view) Author: Filip GruszczyƄski (gruszczy) Date: 2011-04-19 19:42
Nick Coghlan suggested, that operator.getitem could be extended to accept default value. How does it sound to you?
msg134094 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-19 20:00
> operator.getitem could be extended to accept default value

That would be relatively harmless.  And because it isn't type specific to lists, it would be applicable to other types that might better use cases than lists do.

If you want to pursue this, please change the name of this tracker item and re-open it.

Do spend some time thinking about whether it is good language design.  Is "getitem(s, i, dft)" better than "s[i] if i < len(s) else dft" or list unpacking, etc.?  Various meanings of better include 1) the preferred way to do it, 2) a faster way to do it, 3) surrounding code reads significantly better using the new construct, and 4) it's worth taking the time to learn and remember it.  By adding this to the language, you're telling people that this is the RightThingToDo(tm).  

Programming this is easy -- the hard part is knowing whether it is worthwhile (a question on StackOverflow is not sufficient motivation).  You might also want to grep lots of real world code to see if there would be actual benefits to real code or whether it will end-up being cruft that we put in because it was easy and cute.  Python has had 20+ years of development without needing this, so it's worth really thinking about it before going ahead.
History
Date User Action Args
2011-04-22 16:47:47eric.araujosetnosy: + eric.araujo
2011-04-19 20:00:51rhettingersetmessages: + msg134094
2011-04-19 19:42:12gruszczysetmessages: + msg134092
2011-04-19 19:10:57rhettingersetmessages: + msg134090
2011-04-19 18:20:45rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg134088

resolution: rejected
2011-04-19 18:17:09gruszczycreate