Calling dunder methods manually

Serhiy Storchaka storchaka at gmail.com
Thu Apr 13 03:01:16 EDT 2017


On 13.04.17 08:29, Steven D'Aprano wrote:
> Should you call dunder methods (Double leading and trailing UNDERscores)
> manually? For example:
>
>
> my_number.__add__(another_number)
>
>
> The short answer is:
>
> NO! In general, you shouldn't do it.
>
>
> Guido recently commented:
>
>     I agree that one shouldn't call __init__ manually (and in fact Python
>     always reserves the right to have "undefined" behavior when you
>     define or use dunder names other than documented).
>
>
> so unless documented as safe to use manually, you should assume that it
> is not.
>
>
> https://github.com/python/typing/issues/241#issuecomment-292694838

__init__ is perhaps the most called dunder method. It is often called 
from the __init__ method of subclasses.

__add__ also can be called from other __add__, __iadd__ or __radd__.



More information about the Python-list mailing list