とても簡単なことのように感じますが、必要な情報が見つからないようです。クラス Matrix を定義するとします。
class Matrix():
def __mul__(self, other):
if isinstance(other, Matrix):
#Matrix multiplication.
if isinstance(other, int): #or float, or whatever
#Matrix multiplied cell by cell.
これは、行列に int を掛けると問題なく動作しますが、int は行列の処理方法を認識していないため、3*Matrix は TypeError を発生させます。どうすればこれに対処できますか?