On Tue, Jun 22, 2021 at 09:12:53PM +1000, Chris Angelico wrote:

> > The must be no semantic difference between:
> >
> >     obj.method(arg)
> >
> > and
> >
> >     getattr(obj, 'method')(arg)
> >
> > regardless of whether `method` is a regular method or an extension
> > method.
>
> And this is a problem.

If its a problem for getattr, it is a problem for dot syntax, because 
they are essentially the same thing.


> How is getattr defined?

The same as it is defined now, except with some minor tweaks to support 
extension methods.

Do you remember when we introduced `__slots__` (version 2.2 or 2.3, I 
think?), and added a whole new mechanism to look up ordinary attributes 
and slot attributes? No, neither do I, because we didn't.

We have a single mechanism for looking up attributes, including methods, 
which works with instances and classes, descriptors and non-descripters, 
C-level slots and Python `__slots__` and `__dicts__` and `__getattr__` 
and `__getattribute__`, and I am absolutely positively sure that if 
Python ever adds a new implementation for attribute lookup, it will 
still be handled by the same getattr mechanism, which is built into the 
interpreter.


> Is it counted as being in the current module?

`getattr`? No, that's a builtin. You can shadow it or delete it if you 
want, it's just a public API to the underlying functionality built 
into the interpreter. Dot syntax won't be affected.


> If it is, then has getattr magically become
> part of the module it's called from? Or do ALL lookups depend on where
> the function was called, rather than where it's defined?

Can you be a bit more precise? I'm not suggesting that we introduce 
dynamic scoping instead of lexical scoping, if that's what you mean.

Attribute lookups already depend on the state of the object at the time 
the lookup is made. This is just more of the same.

    class K:
        pass

    K.attr  # this is an AttributeError

    K.attr = 'extension'
    K.attr  # this is fine



> If 'method' is an extension method, where exactly is it visible?

I believe that TypeScript uses "import" for this, so it would be visible 
from anywhere that imports it:

https://putridparrot.com/blog/extension-methods-in-typescript/


-- 
Steve
_______________________________________________
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/5CEA6XWEX7VP554RIC4TDTJMURRVAOJL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to