dunder method
A method whose name starts and ends with two underscores, such as __init__ or __len__, which Python calls for you to implement built-in behaviour.
In more detail
Dunder is short for double underscore. len(x) calls x.__len__(), a + b calls a.__add__(b) and == calls __eq__, so defining dunder methods lets your own classes work with Python's operators and built-ins. You rarely call them directly.
Related terms
- __init__ — The dunder method Python calls to set up a newly created instance, receiving the arguments passed to the class.
- __repr__ — __repr__ returns an unambiguous text form of an object for programmers; __str__ returns a friendly one for readers, used by print.
- __eq__ — The dunder method that defines what == means for your objects; without it, two instances are equal only if they are the same object.
- __len__ and __getitem__ — The pair of dunder methods that make your objects work with len(), indexing obj[i] and, as a result, for loops.
Taught in
- Modules And The Import System — Hands-on Python: Complete Video Course & Book
- Packages, And Your Own Library — Hands-on Python: Complete Video Course & Book
- Classes And Instances — Hands-on Python: Complete Video Course & Book
- Dunder Methods, And Making Objects Pythonic — Hands-on Python: Complete Video Course & Book
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Classes, Objects And Two Stacks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Multiple Inheritance, The MRO And Diamonds — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep