Python Notes

Overloading The Addition Operator

Mon 10 August 2026
class Vector:
    def __init__(self, x):
        self.x = x

    def __add__(self, other):
        return Vector(self.x + other.x)

v1 = Vector(5)
v2 = Vector(7)
v3 = v1 + v2

print(v3.x)  # Output: 12
12


Score: 10

Category: chapter5_operator_overloading

Site
  • Archives
  • Tags
Categories
  • chapter1
  • chapter10_modules_and_packages
  • chapter11_csv_handling
  • chapter12_file_handling
  • chapter13_global_and_scope
  • chapter14_main_function
  • chapter1_oop_basics
  • chapter2_inheritance
  • chapter3_multiple_inheritance
  • chapter4_polymorphism
  • chapter5_operator_overloading
  • chapter6_exception_handling
  • chapter7_custom_exceptions
  • chapter8_recursion
  • chapter9_list_comprehension
  • math-work
  • misc
  • pandas-work
Social
  • GitHub
  • LinkedIn