Skip to content

Instantly share code, notes, and snippets.

@ahmedsakr
Created August 3, 2020 05:06
Show Gist options
  • Save ahmedsakr/895a0fedcbd72d507ec32359ad349371 to your computer and use it in GitHub Desktop.
Save ahmedsakr/895a0fedcbd72d507ec32359ad349371 to your computer and use it in GitHub Desktop.
Overriding default __mul__ class implementation
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