what's unsafe to do in a __getattr__?

Dieter Maurer dieter at handshake.de
Mon Oct 25 12:48:24 EDT 2021


Mats Wichmann wrote at 2021-10-24 19:10 -0600:
>Have run into a problem on a "mature" project I work on (there are many
>years of history before I joined), there are a combination of factors
>that combine to trigger a KeyError when using copy.copy().
> ...
>There's a class that's a kind of proxy ...

"Proxying" has become very difficult since the introduction
of Python's "new style classes" and looking up "special methods"
on the type rather then the type instance.

This essentially means that the proxy must implement all
"special methods" that may be relevant to the application.

All special methods start and end with `"__"`.
Your `__getattr__` could raise an `AttributeError` as soon as
it is called with such a name.


More information about the Python-list mailing list