News for hackers. Stuff that matters.
Re:Python idiom for taking the single item from a list by cool-RR on Monday September 27, 2010 @01:50PM (#1732548)
Duck-typing means you don't assume that the object is from a certain type; Instead you assume that it's got the interfaces you need. (Either `.__iter__()` or `.__getitem__(0)` in this case.)

`.__iter__()` is a more general and common interface than `.__getitem__(0)`, so it's preferable to assume that the object implements the former rather than the latter.

But sometimes you do want to assume stuff about your object. In this case we want to assume that the list has exactly one item, and we want our code to break immediately if it isn't true.

There are no comments on this comment yet.