0

Can anybody help me?

Can anybody explain me this code(cannot understand matmul function), please: class l(list): def __matmul__(self,other): new=l() for k in range(len(other)): new.append(self[k]+other[k]) return new i1=l((1,2,3,4,5)) i2=l((9,8,7,6,5)) [email protected] print(sum(i3))

9th Apr 2020, 8:13 PM
Azi 🇺🇿
3 Answers
+ 3
I think you are struggling of __matmul__ and the @-operator. Both are related to matrix multiplication. Find more information at: https://stackoverflow.com/questions/27385633/what-is-the-symbol-for-in-python
9th Apr 2020, 9:01 PM
Lothar
Lothar - avatar
+ 2
Normally the @ operator is used by numpy for matrix multiplication. In your sample code, the meaning of this operator is overridden by the magic method __matmul__ Your class l is subclass of list, with the additional capability that two l objects can be combined with this operator. As it is implemented, it adds the values element-wise to create a new l object, so the result of [email protected] will be [10, 10, 10, 10, 10]
9th Apr 2020, 10:51 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you Tibor Santa
10th Apr 2020, 6:29 AM
Azi 🇺🇿