r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

2

u/loluminium Aug 26 '20

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 => 3

You can also define __div__ to do stuff if you encounter the / operator.