Python – Difference between Python classes and Haskell typeclasses

haskellpython

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?

Best Answer

One difference: In Python, you can define a subclass MySubclass of MyClass, and then you can freely add objects of MyClass with objects of MySubclass.

Haskell tends to avoid subtyping: if you have a typeclass Addable with a function add :: Addable a => a -> a -> a, both summands must always be of the same type. It can be any type that is an instance of Addable, but it must be the same for both summands and the result.

Another difference: in Haskell you can "retroactively" make a type an instance of a typeclass. For example you can create a typeclass and declare instances for some preexisting types that fit the mold. In Python, when you define a class you must list what classes it will extend.

Yet another difference: in Python, "method dispatch" (what method implementation is actually executed in an invocation) is determined by the object receiving the invocation. Haskell typeclasses allow dispatching on the expected return type of a function. A clasic example is the function read :: Read a => String -> a. Depending on the desired type a of the result, different reading functions will be called.

This a good talk on Haskell typeclasses which, from minute 40 onwards, compares them to OO interfaces.

Related Topic