[Python-ideas] LOAD_NAME/LOAD_GLOBAL should be use getattr()
Serhiy Storchaka
storchaka at gmail.com
Wed Sep 13 14:55:58 EDT 2017
12.09.17 19:17, Neil Schemenauer пише:
> This is my idea of making module properties work. It is necessary
> for various lazy-loading module ideas and it cleans up the language
> IMHO. I think it may be possible to do it with minimal backwards
> compatibility problems and performance regression.
>
> To me, the main issue with module properties (or module __getattr__)
> is that you introduce another level of indirection on global
> variable access. Anywhere the module.__dict__ is used as the
> globals for code execution, changing LOAD_NAME/LOAD_GLOBAL to have
> another level of indirection is necessary. That seems inescapable.
>
> Introducing another special feature of modules to make this work is
> not the solution, IMHO. We should make module namespaces be more
> like instance namespaces. We already have a mechanism and it is
> getattr on objects.
There is a difference between module namespaces and instance namespaces.
LOAD_NAME/LOAD_GLOBAL fall back to builtins if the name is not found in
the globals dictionary. Calling __getattr__() will slow down the access
to builtins. And there is a recursion problem if module's __getattr__()
uses builtins.
More information about the Python-ideas
mailing list