Calling dunder methods manually
eryksun at gmail.com
Thu Apr 13 03:18:10 EDT 2017
On Thu, Apr 13, 2017 at 5:29 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> Should you call dunder methods (Double leading and trailing UNDERscores)
> manually? For example:
> 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.
I manually call dunder methods all the time when overriding a special
method in a subclass. It's happening all over the place -- thousands
and even millions of times in a program. You generally shouldn't call
them outside of a class definition -- not because it's inherently bad
(though calling __init__ on an already initialized object is weird and
probably bad), but because it's bad style and does an end-run around
the high-level behavior of the interpreter.
More information about the Python-list