__rmul__在什么情况下被称为? [英] Under what circumstances are __rmul__ called?

查看:218
本文介绍了__rmul__在什么情况下被称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个列表l.在什么情况下称为l.__rmul__(self, other)?

Say I have a list l. Under what circumstance is l.__rmul__(self, other) called?

我基本上理解了文档,但是我也想看看一个示例来毫无疑问地阐明其用法.

I basically understood the documentation, but I would also like to see an example to clarify its usages beyond any doubt.

推荐答案

当Python尝试将两个对象相乘时,它首先尝试调用左侧对象的__mul__()方法.如果左边的对象没有__mul__()方法(或者该方法返回NotImplemented,表明它不适用于所讨论的右边的操作数),那么Python想要知道右边的对象是否可以进行乘法.如果右操作数与左操作数具有相同的类型,Python会知道它不能,因为如果左对象不能做到这一点,那么相同类型的另一个对象当然也不能.

When Python attempts to multiply two objects, it first tries to call the left object's __mul__() method. If the left object doesn't have a __mul__() method (or the method returns NotImplemented, indicating it doesn't work with the right operand in question), then Python wants to know if the right object can do the multiplication. If the right operand is the same type as the left, Python knows it can't, because if the left object can't do it, another object of the same type certainly can't either.

但是,如果两个对象的类型不同,Python则值得一试.但是,如果操作不是可交换的,则需要某种方法来告诉正确的对象它在操作中是正确的对象. (当然,乘法不是全部运算符,而且在任何情况下*都不总是用于乘法!)因此它调用__rmul__()而不是__mul__().

If the two objects are different types, though, Python figures it's worth a shot. However, it needs some way to tell the right object that it is the right object in the operation, in case the operation is not commutative. (Multiplication is, of course, but not all operators are, and in any case * is not always used for multiplication!) So it calls __rmul__() instead of __mul__().

作为示例,请考虑以下两个语句:

As an example, consider the following two statements:

print "nom" * 3
print 3 * "nom"

在第一种情况下,Python调用字符串的__mul__()方法.字符串知道如何将自己乘以整数,因此一切都很好.在第二种情况下,整数不知道如何将自己与字符串相乘,因此其__mul()__返回NotImplemented并调用字符串的__rmul()__.它知道该怎么做,您得到的结果与第一种情况相同.

In the first case, Python calls the string's __mul__() method. The string knows how to multiply itself by an integer, so all is well. In the second case, the integer does not know how to multiply itself by a string, so its __mul()__ returns NotImplemented and the string's __rmul()__ is called. It knows what to do, and you get the same result as the first case.

现在我们可以看到__rmul()__允许 all 字符串的特殊乘法行为包含在str类中,这样其他类型(例如整数)就不需要知道关于字符串的任何事情都可以乘以它们.从现在开始一百年后(假设Python仍在使用中),即使int类在一个多世纪以来一无所知,您仍可以定义一个可以按任一顺序乘以整数的新类型.

Now we can see that __rmul()__ allows all of the string's special multiplication behavior to be contained in the str class, such that other types (such as integers) do not need to know anything about strings to be able to multiply by them. A hundred years from now (assuming Python is still in use) you will be able to define a new type that can be multiplied by an integer in either order, even though the int class has known nothing of it for more than a century.

顺便说一句,字符串类的__mul__()在某些版本的Python中有一个错误.如果它不知道如何将自己与一个对象相乘,则会引发TypeError而不是返回NotImplemented.这意味着即使用户定义的类型具有__rmul__()方法,也不能将字符串乘以用户定义的类型,因为该字符串永远不会让它有机会.必须首先使用用户定义的类型(例如Foo() * 'bar'而不是'bar' * Foo()),以便调用其__mul__().他们似乎已经在Python 2.7中修复了此问题(我也在Python 3.2中对其进行了测试),但是Python 2.6.6有此错误.

By the way, the string class's __mul__() has a bug in some versions of Python. If it doesn't know how to multiply itself by an object, it raises a TypeError instead of returning NotImplemented. That means you can't multiply a string by a user-defined type even if the user-defined type has an __rmul__() method, because the string never lets it have a chance. The user-defined type has to go first (e.g. Foo() * 'bar' instead of 'bar' * Foo()) so its __mul__() is called. They seem to have fixed this in Python 2.7 (I tested it in Python 3.2 also), but Python 2.6.6 has the bug.

这篇关于__rmul__在什么情况下被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆