MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/igvny1/python_goes_brrrr/g2x0ik2/?context=3
r/ProgrammerHumor • u/das_freak • Aug 26 '20
789 comments sorted by
View all comments
2
Why doesn't
'r'/10**(-1)
work?
4 u/Pluckerpluck Aug 26 '20 Actual answer:Because effectively you should look at this as an overloading of the multiplication operator. You can actually do thiswith any custom python object:class MyClass: def __init__(self, x): self.x = x def __mul__(self, other): return MyClass(self.x * other.x) instance_1 = MyClass(12) instance_2 = MyClass(2) instance_3 = instance_1 * instance_2 # instance_3.x => 3You can also define __div__ to do stuff if you encounter the / operator.
4
Actual answer:
Because effectively you should look at this as an overloading of the multiplication operator. You can actually do thiswith any custom python object:
multiplication
class MyClass: def __init__(self, x): self.x = x def __mul__(self, other): return MyClass(self.x * other.x) instance_1 = MyClass(12) instance_2 = MyClass(2) instance_3 = instance_1 * instance_2 # instance_3.x => 3
You can also define __div__ to do stuff if you encounter the / operator.
__div__
/
2
u/loluminium Aug 26 '20
Why doesn't
work?