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

Nathaniel Smith njs at pobox.com
Tue Sep 5 18:52:31 EDT 2017


On Tue, Sep 5, 2017 at 3:03 PM, Larry Hastings <larry at hastings.org> wrote:

I've written a PEP proposing a language change: https://www.python.org/dev/peps/pep-0549/ The TL;DR summary: add support for property objects to modules. I've already posted a prototype.

Interesting idea! It's definitely less arcane than the class assignment support that was added in 3.5. I guess the question is whether to add another language feature here, or to provide better documentation/helpers for the existing feature.

If anyone's curious what the class trick looks like in practice, here's some simple deprecation machinery: https://github.com/njsmith/trio/blob/ee8d909e34a2b28d55b5c6137707e8861eee3234/trio/_deprecate.py#L102-L138

And here's what it looks like in use: https://github.com/njsmith/trio/blob/ee8d909e34a2b28d55b5c6137707e8861eee3234/trio/init.py#L91-L115

Advantages of PEP 549:

Advantages of the class trick:

I don't imagine that I would actually use PEP 549 any time in the foreseeable future, due to the inability to override dir and the minimum version requirement. If you only need to support CPython 3.5+ and PyPy3 5.9+, then you can effectively get PEP 549's functionality at the cost of 3 lines of code and a block indent:

import sys, types class _MyModuleType(types.ModuleType): @property def ...

@property
def ...

sys.modules[name].class = _MyModuleType

It's definitely true though that they're not the most obvious lines of code :-)

-n

-- Nathaniel J. Smith -- https://vorpus.org



More information about the Python-Dev mailing list