To be honest, I was expecting some sort of error, like ‘you can’t rename two nested functions with same name in a same body like that’ , Can we define any number of functions with same name in python ?
In [40]: def add(i,j):
....: def add(i,j):
....: print i+j
....: def add(i,j):
....: print i-j
....: return add(i,j)
....:
In [41]: add(5,4)
1
Is this Overloading of function, or Overriding of function ??
Of course. A
defstatement is functionally an assignment (of the function to the name), and you can have any number of assignments to the same name. (It`s important to realize that function definitions are executed in Python, they are not statically evaluated at compile time.)There are hacky ways to catch this in class definitions, such as this method I posted in May. If you’re using Python 3, there’s a better way.
For functions, that won’t work, so I believe you’re stuck with this behavior.