On Tue, Jun 22, 2021 at 1:44 AM Soni L. <fakedme...@gmail.com> wrote: > > > > On 2021-06-21 12:26 p.m., Stephen J. Turnbull wrote: > > Soni L. writes: > > > > > The trick to extension methods is that they're only available when you > > > explicitly use them. > > > > What does "explicitly use them" mean? How does this help avoid the > > kinds of problems we know that monkey-patching causes? > > Monkey-patching: > > ```py mod1.py > import foo > > foo.Bar.monkeymethod = ... > ``` > > ```py mod2.py > import foo > > foo.Bar.monkeymethod = ... > ``` > > "Extension methods": > > ```py mod1.py > import foo > > def __getattr__(o, attr): > if isinstance(o, foo.Bar) and attr == "monkeymethod": > return ... > return getattr(o, attr) > ``` > > ```py mod2.py > import foo > > def __getattr__(o, attr): > if isinstance(o, foo.Bar) and attr == "monkeymethod": > return ... > return getattr(o, attr) > ``` > > Note how the former changes foo.Bar, whereas the latter only changes the > module's own __getattr__. You can't have conflicts with the latter. > (Also note that this "module's own __getattr__" doesn't provide > extension methods by itself, but can be used as a mechanism to implement > extension methods.)
So what you're saying is that, in effect, every attribute lookup has to first ask the object itself, and then ask the module? Which module? The one that the code was compiled in? The one that is currently running? Both? And how is this better than just using a plain ordinary function? Not everything has to be a method. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/EMA2EBZLIR7DLNJAHWHURATJ4WVBOEUJ/ Code of Conduct: http://python.org/psf/codeofconduct/