News for hackers. Stuff that matters.
Re:Python idiom for taking the single item from a list by jemfinch on Monday September 27, 2010 @02:47PM (#1732840)
The real solution is not to arbitrarily encode your types as lists of exactly one item. If you find yourself passing around such lists with enough regularity that you feel the need to develop an idiom for deconstructing them, you're doing something wrong.
Re:Python idiom for taking the single item from a list by cool-RR on Monday September 27, 2010 @02:52PM (#1732872)
More often than not you are receiving a list from some function, not constructing it manually.

As an example: Say you have a GUI widget that can contain many entries, and you call `widget.get_entries()` which returns a list with all the entries. But if you know there must be only one entry, you can do `(entry,) = widget.get_entries()`.

Re:Python idiom for taking the single item from a list by jemfinch on Monday September 27, 2010 @03:14PM (#1732953)
Then it should make a method which returns its known, sole entry. Returning a list of entries from a widget which will always contain only one entry is the logical equivalent to converting a function return value to a string and then expecting the client to convert it back from a string. That is to say, it's necessary in a general case (e.g., a Widget super class) but should not be exposed that way in some specific cases (e.g., your subclass wherein you know there will always be one entry).

In reality, you shouldn't be mucking with the entries of a widget at all; you should tell the widget what to do and it should adjust its entries as necessary). Demeter's law and all.

Re:Python idiom for taking the single item from a list by j_baker on Monday September 27, 2010 @03:42PM (#1733051)
I notice a lot of "shoulds" in this comment. Unfortunately, what should be and what is aren't always the same thing.
Re:Python idiom for taking the single item from a list by jemfinch on Monday September 27, 2010 @04:38PM (#1733308)
No doubt! But since we're on the subject of "how to make code like this better" it seems appropriate to discuss what's really wrong with the code, rather than just what sort of duct tape style guidelines we can use to patch over its flaws :)
Re:Python idiom for taking the single item from a list by xiongchiamiov on Monday September 27, 2010 @04:54PM (#1733371)
Very often we can't change the API (standard library or such). Should we not change our code to address the issue as best we can?
Re:Python idiom for taking the single item from a list by swolchok on Monday September 27, 2010 @05:24PM (#1733500)
This is Python. Subclass it and fix the problem, unless the guts are so opaque that your code would be littered with subclasses and you can't figure out an elegant, general way of fixing it (unlikely).
Re:Python idiom for taking the single item from a list by tmsh on Monday September 27, 2010 @08:09PM (#1734066)
This is Sparta.

  class SpartanList(list):
       def foot(self):
          return self[0]


  assert l.foot()
  return l.foot()