Python 2.7 实例对象的串联,是否应该排序?

Python 2.7 实例对象的串联,是否应该排序?,python-2.7,Python 2.7,在创建了一个简单的类并对其进行了实例化之后,我发现了这种好奇心 class A(): def __init__(self,inp): self.id = inp def get_id(self): return self.id def __add__(self,other): return other + self.id 以及实例化 me = A('Matt') 现在,当我尝试使用重载方法\uuu

在创建了一个简单的类并对其进行了实例化之后,我发现了这种好奇心

class A():
    def __init__(self,inp):
            self.id = inp
    def get_id(self):
            return self.id
    def __add__(self,other):
            return other + self.id
以及实例化

me = A('Matt')
现在,当我尝试使用重载方法
\uuuuu add\uuuuu
时,它会按预期工作

me + ' test concatenation'   
但当我用另一种方式做时,
“测试串联”+me
,我得到了

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'instance' objects
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法连接'str'和'instance'对象

为什么参数的顺序很重要?

因为“+”符号隐式地首先调用

在封面下,当你有一个像“hello”+“world”这样的表达式时,你实际上是在执行这个:string。因此,uuu add_uuu方法是第一个参数的方法,这就是顺序重要的原因