Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Python Fundamentals

Lesson 29 of 55 · Python

Magic Methods and Dunder

Duration: 20 minutes

Magic Methods and Dunder\n\nMagic (or dunder) methods are special hooks that let your objects integrate with Python’s built‑in syntax. They always start and end with double underscores (__).\n\npython\nclass Vector:\n def __init__(self, x, y):\n self.x = x\n self.y = y\n def __repr__(self):\n return f'Vector({self.x}, {self.y})'\n def __len__(self):\n return 2\n def __add__(self, other):\n return Vector(self.x + other.x, self.y + other.y)\n\nv1 = Vector(1, 2)\nv2 = Vector(3, 4)\nprint(v1) # Vector(1, 2)\nprint(len(v1)) # 2\nprint(v1 + v2) # Vector(4, 6)\n\n\nImplementing the appropriate dunder methods makes your classes feel like native Python types.\n

Previous: OOP – EncapsulationNext: Decorators