Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud
by Paul J. Deitel, Harvey M. Deitel
10.10 Operator Overloading
You’ve seen that you can interact with objects by accessing their attributes and properties and by calling their methods. Method-call notation can be cumbersome for certain kinds of operations, such as arithmetic. In these cases, it would be more convenient to use Python’s rich set of built-in operators.
This section shows how to use operator overloading to define how Python’s operators should handle objects of your own types. You’ve already used operator overloading frequently across wide ranges of types. For example, you’ve used:
the
+operator for adding numeric values, concatenating lists, concatenating strings and adding a value to every element in a NumPy array.the
[]operator for accessing elements in lists, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access