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

Nathaniel Smith njs at pobox.com
Wed Sep 13 17:00:13 EDT 2017


On Wed, Sep 13, 2017 at 11:49 AM, Guido van Rossum <guido at python.org> wrote:

> Why not adding both? Properties do have their uses as does getattr.

In that case I would just add getattr to module.c, and add a recipe or perhaps a utility module that implements a getattr you can put into your module if you want @property support. That way you can have both but you only need a little bit of code in module.c to check for getattr and call it when you'd otherwise raise AttributeError.

Unfortunately I don't think this works. If there's a @property object present in the module's instance dict, then getattribute will return it directly instead of calling getattr.

(I guess for full property emulation you'd also need to override setattr and dir, but I don't know how important that is.)

We could consider letting modules overload getattribute instead of getattr, but I don't think this is viable either -- a key feature of getattr is that it doesn't add overhead to normal lookups. If you implement deprecation warnings by overloading getattribute, then it makes all your users slower, even the ones who never touch the deprecated attributes. getattr is much better than getattribute for this purpose.

Alternatively we can have a recipe that implements @property support using class assignment and overriding getattribute/setattr/dir, so instead of 'from module_helper.property_emulation import getattr' it'd be 'from module_helper import enable_property_emulation; enable_property_emulation(name)'. Still has the slowdown problem but it would work.

-n

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



More information about the Python-Dev mailing list