52

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
ibz
  • 44,461
  • 24
  • 70
  • 86
  • 9
    These are often called "magic methods." That might help with google. – balpha Jul 07 '09 at 06:10
  • 4
    "Magic"? Where'd you get that? The Python documentation calls them "special method names". I've never seen them called "magic". Where did you see that? – S.Lott Jul 07 '09 at 12:51
  • 6
    @S.Lott No? Haven't you [readen the docs](http://docs.python.org/whatsnew/2.1.html?highlight=magic#pep-207-rich-comparisons)? Haven't you readen [the guide](http://www.rafekettler.com/magicmethods.html) to them? – Alba Mendez Jul 17 '12 at 15:47
  • 8
    I don't understand why this is closed. The question is useful and constructive and the answer is supported by reference. There is nothing to debate or argue for this question. I don't see this violating any of the rules in help center. – ketorin Sep 12 '13 at 07:30
  • 1
    Here is the excellent link which explain python's magic method in detail [link](http://www.rafekettler.com/magicmethods.html) – Tushar Patil Jan 29 '14 at 11:14
  • 5
    @ketorin: The reason why good questions like this get closed is because it is fun for people who have been granted shiny new powers to exercise them. – OCDev Feb 04 '14 at 14:47
  • Couldn't find any documentation for these, but this gives a good overview: http://www.rafekettler.com/magicmethods.html – endolith Aug 12 '14 at 03:41
  • 1
    @OCDev then why haven't people reopened it? – tox123 Sep 24 '16 at 17:56
  • 1
    @tox123 - It remains closed because the mental barrier is higher to go against an established precedent than it is to set a precedent where one had not been established yet. Can you tell us why it makes sense for this question to have been closed? – OCDev Sep 27 '16 at 09:40

1 Answers1

52

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
  • 78,318
  • 8
  • 63
  • 70
  • 2
    +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 Jul 07 '09 at 12:53
  • 1
    Another [helpful link](http://www.rafekettler.com/magicmethods.html) – Raghu Oct 08 '12 at 13:30