views:

846

answers:

4

What are all the special (magic) methods in Python? The __xxx__ methods, that is.

I'm often looking for a way to override something which I know is possible to do through one of these methods, but I'm having a hard time to find how since as far as I can tell there is no definitive list of these methods, PLUS their names are not really Google friendly. So I think having a list of those here on SO would be a good idea.

+7  A: 

This page lists all the "underscore" methods:

http://www.siafoo.net/article/57

It's a handy reference.

ars
+18  A: 

At the python level, most of them are documented in the language reference. At the C level, you can find it under the object protocol section (strictly speaking, you only have a subset here, though).

David Cournapeau
+1: Every single one, clearly listed. Google "Python Special Method Names" and you get 700,000 reponses starting with section 3. of the Language Reference.
S.Lott
+1  A: 

Python Quick Reference Card has a somewhat comprehensive list of those scattered across it.

lhahne
Unfortunately it doesn't appear to have been updated since Python 2.4.
Scott Griffiths
I don't think they've changed very much.
lhahne
A: 

Michael Foord wrote about them in its IronPython in Action.
The appendix with that reference is available online and while focusing on IronPython, is valuable for CPython users as well.

Roberto Liffredo