"If Python doesn't support overloading, how does '+' work which can be either addition or concatenation, correct?"
My collegue Peter asked me this once. Let me explain this question, then answer it. And it starts with the word "overloading", which has two different meanings in Python.
When you write a class in Python, you can have only one version of each method. For example, this doesn't work:
import math
class Point :
def __init__ ( self , x , y ):
self . x , self . y = x , y
def distance ( self , other_point ):
# Distance from this point to another point
return math . sqrt (
( self . x - other_point . x ) ** 2 +
( self . y - other_point . y ) ** 2 )
def distance ( self , x , y ):
# Distance from this point to other x-y coords
return math . sqrt ( ( self . x - x ) ** 2 + ( self . y - y ) ** 2 )
See the two distance() methods? See how it's defined twice?
In Python, only the second version is kept. The first one is silently overwritten and erased.…