2012-04-21 58 views
0

在python 3中,我想在我創建的類中進行操作符重載。與__add__相反嗎?

我知道__add__代表operator +。

什麼是-*^|&的方法呢?

+0

您可以通過點擊它附近的綠色對號接受ThiefMaster的答案。 – 2012-04-23 17:41:25

回答

8

請參閱the python docs on the data model

你問的方法是:__sub____mul____xor____or____and__

以下是完整名單:

object.__add__(self, other) 
object.__sub__(self, other) 
object.__mul__(self, other) 
object.__truediv__(self, other) 
object.__floordiv__(self, other) 
object.__mod__(self, other) 
object.__divmod__(self, other) 
object.__pow__(self, other[, modulo]) 
object.__lshift__(self, other) 
object.__rshift__(self, other) 
object.__and__(self, other) 
object.__xor__(self, other) 
object.__or__(self, other) 
+0

謝謝!!!幫助我很多! (: – Jjang 2012-04-21 17:10:46

+5

@Jjang:然後請點擊左側的複選標記圖標接受此答案。 – ThiefMaster 2012-04-21 17:37:17