Is "Changing the behavior of an operator like + so it works with a programmer-defined type," a good definition of operator overloading?

In "Think Python: How to Think Like a Computer Scientist", the author defines operator overloading as:

Changing the behavior of an operator like + so it works with a programmer-defined type.

Is this an accurate definition of it (in programming in general, and in Python in specific)? Isn't it: "The ability to use the same operator for different operations?" For example, in Python, we an use + to add to numbers, or to concatenate two sequences. Isn't this operator overloading? Isn't the + operator overloaded here?

Does the author means by "the behavior of an operator", raising a TypeError because it's not implemented for the given class? Because the operator still has its behavior with other types (e.g. strings).

Is the definition the author wrote, a specific type of operator overloading?

Upvotes: 0

Views: 77

Answers (1)

Jacques de Hooge
Jacques de Hooge

Reputation: 7000

The definition given in "How to think..." is correct. It isn't specific for Python, C++ has the same concept.

The programmer can give an operator a new meaning, e.g. adding two vectors with a + instead of two scalar numbers.

The mere fact that an operator can be used on multiple datatypes natively doesn't have a specific naming. In almost any language + can be used to add integers or floats. There's no special word for this and many programmers aren't even aware of the difference.

Upvotes: 1

Related Questions