[Python-Dev] PEP 549: Instance Properties (aka: module properties)

Brett Cannon brett at python.org
Wed Sep 20 17:44:41 EDT 2017


On Thu, 14 Sep 2017 at 12:09 Ivan Levkivskyi <levkivskyi at gmail.com> wrote:

> On 14 September 2017 at 01:13, Guido van Rossum <guido at python.org> wrote:
>
>>
>> That last sentence is a key observation. Do we even know whether there
>> are (non-toy) things that you can do *in principle* with __class__
>> assignment but which are too slow *in practice* to bother? And if yes, is
>> __getattr__ fast enough? @property?
>>
>>
> I myself have never implemented deprecation warnings management nor lazy
> loading,
> so it is hard to say if __class__ assignment is fast enough.
>

For lazy loading it is, but that's because after the lazy load,
module.__class__ is set back to its original value, so any performance
penalty is paid only once upon triggering __getattribute__ on the lazy
module.

IOW no one has complained and I know plenty of places using the lazy
loading mechanism in importlib at scale. :)

-Brett


> For me it is more combination
> of three factors:
>
> * modest performance improvement
> * some people might find __getattr__ clearer than __class__ assignment
> * this would be consistent with how stubs work
>
>
>> IMO we're still looking for applications.
>>
>>
> How about this
>
> def allow_forward_references(*allowed):
>     caller_globals = sys._getframe().__globals__
>     def typing_getattr(name):
>         if name in allowed:
>             return name
>         raise AttributeError(...)
>     caller_globals.__getattr__ = typing_getattr
>
> from typing_extensions import allow_forward_references
> allow_forward_references('Vertex', 'Edge')
>
> T = TypeVar('T', bound=Edge)
>
> class Vertex(List[Edge]):
>     def copy(self: T) -> T:
>         ...
>
> class Edge:
>     ends: Tuple[Vertex, Vertex]
>     ...
>
> Look mum, no quotes! :-)
>
> --
> Ivan
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/brett%40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170920/4d809a80/attachment.html>


More information about the Python-Dev mailing list