为什么 Python 使用“魔术方法”?
我对 Python 广泛使用“魔术方法”感到有点惊讶。
例如,为了让类声明实例具有“长度”,它实现了一个 __len__ 方法,当您编写 len(obj) 时会调用该方法。为什么不直接定义一个作为对象成员直接调用的len
方法,例如obj.len()
?
I'm a bit surprised by Python's extensive use of 'magic methods'.
For example, in order for a class to declare that instances have a "length", it implements a __len__
method, which it is called when you write len(obj)
. Why not just define a len
method which is called directly as a member of the object, e.g. obj.len()
?
See also: Why does Python code use len() function instead of a length method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
AFAIK,
len
在这方面很特殊并且有历史根源。以下是来自常见问题解答的引用:
其他“神奇方法”(实际上在 Python 民间传说中称为特殊方法)很有意义,并且其他语言中也存在类似的功能。它们主要用于在使用特殊语法时隐式调用的代码。
例如:
等等...
AFAIK,
len
is special in this respect and has historical roots.Here's a quote from the FAQ:
The other "magical methods" (actually called special method in the Python folklore) make lots of sense, and similar functionality exists in other languages. They're mostly used for code that gets called implicitly when special syntax is used.
For example:
and so on...
来自 Python 之禅:
这是原因之一 - 使用自定义方法,开发人员可以自由选择不同的方法名称,例如
getLength()
、length()
、getlength( )
或其他任何内容。 Python 强制执行严格的命名,以便可以使用通用函数 len() 。许多类型对象常见的所有操作都放入魔术方法中,例如 __nonzero__ 、 __len__ 或 __repr__ 。不过,它们大多是可选的。
运算符重载也是通过魔术方法(例如
__le__
)完成的,因此将它们用于其他常见操作也是有意义的。From the Zen of Python:
This is one of the reasons - with custom methods, developers would be free to choose a different method name, like
getLength()
,length()
,getlength()
or whatsoever. Python enforces strict naming so that the common functionlen()
can be used.All operations that are common for many types of objects are put into magic methods, like
__nonzero__
,__len__
or__repr__
. They are mostly optional, though.Operator overloading is also done with magic methods (e.g.
__le__
), so it makes sense to use them for other common operations, too.Python 使用“魔术方法”这个词,因为这些方法确实为您的程序发挥了魔力。使用 Python 魔术方法的最大优点之一是它们提供了一种简单的方法来使对象表现得像内置类型。这意味着您可以避免使用丑陋、违反直觉和非标准的方式来执行基本运算符。
考虑以下示例:
这会产生错误,因为字典类型不支持加法。现在,让我们扩展字典类并添加 "__add__" 魔术方法:
现在,它给出以下输出。
因此,通过添加此方法,神奇的事情突然发生了,您之前遇到的错误也消失了。
我希望,它能让你清楚地了解事情。有关更多信息,请参阅:
Python 魔法方法指南(Rafe Kettler,2012)
Python uses the word "magic methods", because those methods really performs magic for you program. One of the biggest advantages of using Python's magic methods is that they provide a simple way to make objects behave like built-in types. That means you can avoid ugly, counter-intuitive, and nonstandard ways of performing basic operators.
Consider a following example:
This gives an error, because the dictionary type doesn't support addition. Now, let's extend dictionary class and add "__add__" magic method:
Now, it gives following output.
Thus, by adding this method, suddenly magic has happened and the error you were getting earlier, has gone away.
I hope, it makes things clear to you. For more information, refer to:
A Guide to Python's Magic Methods (Rafe Kettler, 2012)
其中一些函数的功能超出了单个方法所能实现的功能(在超类上没有抽象方法)。例如,
bool()
的行为有点像这样:您还可以 100% 确定
bool()
将始终返回 True 或 False;如果您依赖某种方法,您就无法完全确定会得到什么。其他一些实现相对复杂的函数(可能比底层魔术方法更复杂)是
iter()
和cmp()
,以及所有属性方法 (getattr
、setattr
和delattr
)。像int
这样的东西在进行强制转换时也会访问魔术方法(您可以实现__int__
),但作为类型执行双重任务。len(obj)
实际上是我认为它与obj.__len__()
没有什么不同的一种情况。Some of these functions do more than a single method would be able to implement (without abstract methods on a superclass). For instance
bool()
acts kind of like this:You can also be 100% sure that
bool()
will always return True or False; if you relied on a method you couldn't be entirely sure what you'd get back.Some other functions that have relatively complicated implementations (more complicated than the underlying magic methods are likely to be) are
iter()
andcmp()
, and all the attribute methods (getattr
,setattr
anddelattr
). Things likeint
also access magic methods when doing coercion (you can implement__int__
), but do double duty as types.len(obj)
is actually the one case where I don't believe it's ever different fromobj.__len__()
.它们并不是真正的“神奇名字”。它只是对象必须实现以提供给定服务的接口。从这个意义上说,它们并不比您必须重新实现的任何预定义接口定义更神奇。
They are not really "magic names". It's just the interface an object has to implement to provide a given service. In this sense, they are not more magic than any predefined interface definition you have to reimplement.
虽然原因主要是历史性的,但 Python 的 len 中有一些特殊性,使得使用函数而不是方法更合适。
Python 中的一些操作被实现为方法,例如
list.index
和dict.append
,而其他操作则被实现为可调用函数和魔术方法,例如str< /code> 和
iter
以及反转
。这两个群体差异很大,因此采用不同的方法是合理的:str
、int
和朋友都是类型。调用构造函数更有意义。__iter__
不可用,则iter
可能会调用__getitem__
,并且支持不适合方法调用的其他参数。出于同样的原因,在最新版本的 Python 中,it.next()
已更改为next(it)
- 它更有意义。__iter__
和__next__
的语法 - 它称为for
循环。为了一致性,函数更好。它可以更好地进行某些优化。repr
的作用类似于str
的作用。使用str(x)
与x.repr()
会令人困惑。isinstance
。getattr(x, 'a')
是执行xa
的另一种方式,并且getattr
具有许多上述品质。我个人将第一组称为“类方法”,将第二组称为“类运算符”。这不是一个很好的区别,但我希望它能有所帮助。
话虽如此,
len
并不完全适合第二组。它更接近第一个操作,唯一的区别是它比几乎所有操作都更常见。但它唯一做的就是调用__len__
,并且它非常接近L.index
。然而,也存在一些差异。例如,可能会调用__len__
来实现其他功能,例如bool
,如果调用该方法len
,您可能会破坏>bool(x)
使用自定义的len
方法来完成完全不同的事情。简而言之,您有一组非常常见的功能,类可以实现这些功能,这些功能可以通过运算符、特殊函数(通常比实现执行更多操作,就像运算符那样)、在对象构造期间以及所有这些功能进行访问具有一些共同特征。剩下的都是方法。而
len
在某种程度上是该规则的一个例外。While the reason is mostly historic, there are some peculiarities in Python's
len
that make the use of a function instead of a method appropriate.Some operations in Python are implemented as methods, for example
list.index
anddict.append
, while others are implemented as callables and magic methods, for examplestr
anditer
andreversed
. The two groups differ enough so the different approach is justified:str
,int
and friends are types. It makes more sense to call the constructor.iter
might call__getitem__
if__iter__
isn't available, and supports additional arguments that don't fit in a method call. For the same reasonit.next()
has been changed tonext(it)
in recent versions of Python - it makes more sense.__iter__
and__next__
- it's called thefor
loop. For consistency, a function is better. And it makes it better for certain optimisations.repr
acts likestr
does. Havingstr(x)
versusx.repr()
would be confusing.isinstance
.getattr(x, 'a')
is another way of doingx.a
andgetattr
shares many of the aforementioned qualities.I personally call the first group method-like and the second group operator-like. It's not a very good distinction, but I hope it helps somehow.
Having said this,
len
doesn't exactly fit in the second group. It's more close to the operations in the first one, with the only difference that it's way more common than almost any of them. But the only thing that it does is calling__len__
, and it's very close toL.index
. However, there are some differences. For example,__len__
might be called for the implementation of other features, such asbool
, if the method was calledlen
you might breakbool(x)
with customlen
method that does completely different thing.In short, you have a set of very common features that classes might implement that might be accessed through an operator, through a special function (that usually does more than the implementation, as an operator would), during object construction, and all of them share some common traits. All the rest is a method. And
len
is somewhat of an exception to that rule.上面两篇文章没有太多要补充的内容,但所有“神奇”功能其实根本就不神奇。它们是 __builtins__ 模块的一部分,在解释器启动时隐式/自动导入。即:
每次在程序开始之前都会发生。
我一直认为,如果 Python 只为交互式 shell 执行此操作,并且需要脚本从所需的内置函数中导入各个部分,那会更正确。另外,在 shell 和交互式中,不同的 __ main__ 处理可能会更好。不管怎样,检查一下所有的功能,看看没有它们会是什么样子:
There is not a lot to add to the above two posts, but all the "magic" functions are not really magic at all. They are part of the __ builtins__ module which is implicitly/automatically imported when the interpreter starts. I.e.:
happens every time before your program starts.
I always thought it would be more correct if Python only did this for the interactive shell, and required scripts to import the various parts from builtins they needed. Also probably different __ main__ handling would be nice in shells vs interactive. Anyway, check out all the functions, and see what it is like without them: