Main Content

Overloading of operators on standard objects in Python

Archive - Originally posted on "The Horse's Mouth" - 2005-07-19 17:58:22 - Graham Ellis

In python, everything is an object and operations performed by operators such as + and * vary in exactly what they do based on the class (type) or objects on which they're run.

If I use the * operator on a list, it replicates it ... but if I use it on an integer, it multiplies it ...


apple = [17]
orange = 20

firstfruit = apple * 7
secondfruit = orange * 7

print firstfruit
print secondfruit


Results:


[17, 17, 17, 17, 17, 17, 17]
140