Actually, Python needs two different functions to simulate multiple dispatch (dispatch on both arguments of +), and it only works for a few “magic” functions. This is discussed in @jeff.bezanson’s PhD thesis:
Numbers tend to be among the most complex features of a language. Numeric types usually need to be a special case: in a typical language with built-in numeric types, describing their behavior is beyond the expressive power of the language itself. For example, in C arithmetic operators like + accept multiple types of arguments (ints and floats), but no user-defined C function can do this (the situation is of course improved in C++). In Python, a special arrangement is made for + to call either an __add__ or __radd__ method, effectively providing double-dispatch for arithmetic in a language that is idiomatically single-dispatch.
In general, Python functions are only “single-dispatch” — when you call obj.method(args...), it only looks at the type of obj. Python provides a somewhat crude form of double dispatch for overloading a few binary operators like + and *, but does this by requiring you to implement two “magic methods” (such as __add__ and __radd__) in general.
In one Julia tutorial I saw the @code_llvm macro is used to show what code is emitted by Julia for a simple function. You can see how multiple dispatch emits code for each type of the arguments.
Please - if anyone can give a link to this tutorial I would be grateful.
julia> f(a::Int64, b::Int64) = 2x + y
f (generic function with 1 method)
julia> f(a::Float64, b::Float64) = 2x-y
f (generic function with 2 methods)
julia> @which f(1,1)
f(a::Int64, b::Int64) in Main at REPL:1
julia> @which f(1.0,1.0)
f(a::Float64, b::Float64) in Main at REPL:1
but when user defines his own functions in Python, he can make multiple arguments(Below example, multiple arguments are ‘a’ and ‘b’) as input parameters and also is able to set both types. For example,
def f(a:int, b:int):
I used 1.0 as an input parameter which is not an int type, but still got an answer.
It shows that python does not care about the type unlike Julia anyway.
What do you mean that Python can only do the first arguments’ types into account?
So, I’m not very good at Python, but from what I understand you can have multiple methods of the same function if you create them as “instance methods”. So foo.bar(baz) chooses a method based on the type of foo. In Julia, this would be bar(foo, baz), and you can have different methods depending on the types of both foo and baz.
I don’t know how well it works. But the things that make multiple dispatch great in Julia are that it is used everywhere, and that it is fast, so there’s no performance penalty for using it. I don’t know how efficient the python implementation is, but MD is certainly not going to be pervasive in Python for a long time, if ever.
I really suggest editing out the medium article, it completely misses the point of what makes multiple dispatch “multiple” and shows only single-argument examples. If I did not know Julia and wanted to defend the use of Python in my lab/office, I would use it as a prime example of Julia people do not knowing of what they talk about.