Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23198

Python Morsels: Every dunder method in Python

$
0
0

An explanation of all of Python's 100+ dunder methods and 50+ dunder attributes, including a summary of each one.

The 3 essential dunder methods 🔑

There are 3 dunder methods that most classes should have: __init__, __repr__, and __eq__.

OperationDunder Method CallReturns
T(a, b=3)T.__init__(x, a, b=3)None
repr(x)x.__repr__()str
x == yx.__eq__(y)Typically bool

The __init__ method is the initializer (not to be confused with the constructor), the __repr__ method customizes an object's string representation, and the __eq__ method customizes what it means for objects to be equal to one another.

The __repr__ method is particularly helpful at the the Python REPL and when debugging.

Equality and hashability 🟰

In addition to the __eq__ …

Read the full article: https://www.pythonmorsels.com/every-dunder-method/


Viewing all articles
Browse latest Browse all 23198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>