Rafe Kettler, a developer, wrote a guide to Python's magic methods and posts on design and the LulzSec password leak.
HN links it for the magic methods guide, cited as a reference, though one commenter says some of its patterns are incomplete or frowned upon.
Honestly, I'm not sure if you can do that in Python - one of the side-effects of not writing classes in Python is that I'm also ignorant of the behind-the-scenes stuff that goes on with them.
You may be able to get something close to what I wrote with a function that returns a closure, but I don't think you can get exactly the for x in y syntax. As explained in another post on HN's front page, "A Guide to Python's Magic Methods": http://www.rafekettler.com/magicmethods.html#sequence, you need to define the __iter__(self) method on an object to support that style of iteration. (And that method needs to return an iterator object.)
And that brings us back to the same point I already made, but I think it's so important that I'll say it again: the abstractions you choose to represent concepts in your program will be determined by what is easy in the language you're using.
Methods taking "self" as an argument is a dirty hack, ain't no magic there. The language is just pretty ugly. It's got the whole whitespace thing going on, which is forced, and some might think it gives a good aesthetic but that is dust in the wind in terms of help for Python's ease on the eyes.
The whole standard namespacing and mix of oop/procedural global functions is just..schizophrenic, the capitalization scheme is dastardly. I haven't seen many languages that look more _basic_ than python. It's a frankenstein of Perl/Ruby and Basic. Forced verbosity is egh.
Here's your magic methods and properties: http://www.rafekettler.com/magicmethods.html
Those magic methods kind of remind me of clever hacks while coding in PHP.
Several of those patterns are incomplete or frowned upon:
* if a method does not use the object's state (no `self` usage) make it a `class-` or `staticmethod`.
* Some magic methods are presented. There's more to them[0].
* one should not write `class MyClass:` but `class MyClass(object):` (new style class[1])
* the last one (`return None`) make me very dubious
* Cascading methods: that's a big no. The idiom is that if a method may change the state of the object then it should return None (eg `set.add`)
0: well-written and comprehensive guide: http://www.rafekettler.com/magicmethods.html
1: http://www.python.org/doc/newstyle/
The ongoing obsession with direct to github store links is irritating. Why not post a link to a designed start of reading point like: http://www.rafekettler.com/magicmethods.html ? Or to https://github.com/RafeKettler/magicmethods/raw/master/magicmethods.pdf so we can share the pleasure of the Scribd cabal screwing up the link and "annoyance wall" the content.
More than just built in functions, any object that defines a __len__ and a __index__ method (that provide len(x) and x[0] respectively) can also be iterated over in a for loop ('for item in instance'). A good reference for magic methods is [1] for anyone interested.
1. http://www.rafekettler.com/magicmethods.html
I'd love other answers to the same question. Here are a few I've come across in the past:
http://www.rafekettler.com/magicmethods.html
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
http://www.siafoo.net/article/52