Python 中的特殊(魔法)方法 [英] Special (magic) methods in Python
问题描述
Python 中有哪些特殊(神奇)的方法?__xxx__
方法,即.
What are all the special (magic) methods in Python? The __xxx__
methods, that is.
我经常寻找一种方法来覆盖我知道可以通过其中一种方法完成的事情,但是我很难找到方法,因为据我所知没有明确的列表在这些方法中,加上它们的名字并不是真正的 Google 友好的.所以我认为在 SO 上列出这些人的名单是个好主意.
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.
推荐答案
在 python 级别,大部分都记录在 语言参考.在 C 层,你可以在 object protocol 部分下找到它(严格来说,不过你这里只有一个子集).
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).
这篇关于Python 中的特殊(魔法)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!