Created
August 3, 2020 05:06
-
-
Save ahmedsakr/895a0fedcbd72d507ec32359ad349371 to your computer and use it in GitHub Desktop.
Overriding default __mul__ class implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DPVector(tuple): | |
''' | |
Dot Product (DP) Vector class that overrides the __mul__ | |
implementation to perform dot product operation! | |
''' | |
def __init__(self, tup): | |
self.tup = tup | |
def __mul__(self, other): | |
''' | |
Python will call this __mul__ whenever you attempt to multiply | |
an object of this class with another. | |
''' | |
return self.tup[0] * other[0] + self.tup[1] * other[1] | |
v = DPVector((1,2)) | |
y = DPVector((3,4)) | |
# output: 11 | |
print (v * y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment