operator overloading and python' special methods python' built-in classes provide natural semantics for many operators for examplethe syntax invokes addition for numeric typesyet concatenation for sequence types when defining new classwe must consider whether syntax like should be defined when or is an instance of that class by defaultthe operator is undefined for new class howeverthe author of class may provide definition using technique known as operator overloading this is done by implementing specially named method in particularthe operator is overloaded by implementing method named add which takes the right-hand operand as parameter and which returns the result of the expression that isthe syntaxa bis converted to method call on object of the forma add (bsimilar specially named methods exist for other operators table provides comprehensive list of such methods when binary operator is applied to two instances of different typesas in love me python gives deference to the class of the left operand in this exampleit would effectively check if the int class provides sufficient definition for how to multiply an instance by stringvia the mul method howeverif that class does not implement such behaviorpython checks the class definition for the right-hand operandin the form of special method named rmul ( "right multiply"this provides way for new user-defined class to support mixed operations that involve an instance of an existing class (given that the existing class would presumably not have defined behavior involving this new classthe distinction between mul and rmul also allows class to define different semantics in casessuch as matrix multiplicationin which an operation is noncommutative (that isa may differ from anon-operator overloads in addition to traditional operator overloadingpython relies on specially named methods to control the behavior of various other functionalitywhen applied to user-defined classes for examplethe syntaxstr(foo)is formally call to the constructor for the string class of courseif the parameter is an instance of userdefined classthe original authors of the string class could not have known how that instance should be portrayed so the string constructor calls specially named methodfoo str )that must return an appropriate string representation similar special methods are used to determine how to construct an intfloator bool based on parameter from user-defined class the conversion to boolean value is particularly importantbecause the syntaxif foo:can be used even when foo is not formally boolean value (see section for user-defined classthat condition is evaluated by the special method foo bool