Planet Python

Python Morsels: Don't call dunder methods

In Python, a method with two underscores around it is referred to as a "dunder method", which stands for "double-underscore method". The term "dunder method" is not an official term, but rather a colloquialism. Officially, these methods are referred to as "special methods". Dunder methods allow Python programmers to customize the behavior of built-in Python behaviors. They act as a sort of "hook" to modify Python's default behavior. It's common to define dunder methods on classes, but uncommon to call them directly. Calling dunder methods directly is generally discouraged, as it can lead to unexpected behavior. However, there are situations where calling dunder methods is necessary. Despite this, it's still important to define dunder methods on classes to customize behavior. Overall, it's best to avoid calling dunder methods, but do define them to take advantage of Python's customization capabilities.
favicon
pythonmorsels.com
pythonmorsels.com