Examples of Dunder Methods
What are some examples of dunder methods in Python, and when are they called?
Answer
Common examples of dunder methods include:
__init__: called when an object is instantiated.__str__: called bystr()orprint()to get a readable string representation.__len__: called bylen().__getitem__: called when using indexing or slicing.__iter__and__next__: called during iteration (e.g., in afor-loop).__add__: called for the+operator.