I'm an intermediate Python dev and just starting out with Haskell. It seems like Python's classes can be used in a way that is similar to how Haskell's typeclasses are used. It seems like typeclasses are just a way to implement operator overloading. Like this:
# main.py
class MyClass:
def __init__(self, num, name):
self.num = num
self.name = name
def __str__(self):
return self.name
def __radd__(self, other):
self.num + other
def __add__(self, other):
self.num + other
Now when you do something like
# main.py
c = MyClass(1, 'hello')
c2 = MyClass(2, 'foo')
str(c) # 'hello'
sum([c, c2]) # 3
Now lets compare this to Haskell:
-- main.hs
data MyClass = Foo | Bar
instance Show MyClass where
show Foo = "foo"
show Bar = "hahahahaha"
Now I can do something like:
show Bar -- "hahahaha"
My newbie (at best) knowledge tells me that the only advantage to using Haskell (if one is comparing only these two language features) is that the Haskell programmer is capable of defining n
"operators" to overload whereas Python has a fixed list
But this cannot be the whole story. What are the other differences? What am I missing here?