This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients Arfrever, daniel.urban, eric.snow, ezio.melotti, flox, jcea, ncoghlan
Date 2013-03-16.13:16:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1363439814.43.0.0148769670132.issue17044@psf.upfronthosting.co.za>
In-reply-to
Content
Eric and I discussed this further this morning. We were interested in two main points:
1. When no custom namespace is used, we definitely shouldn't double the storage requirements for the class object
2. If a custom namespace is used, it would be nice to make it available to both the __init_class__ hook *and* to class decorators

The design we came up with for doing so is for type.__new__ to include the equivalent of:

    if type(namespace) != dict:
        cls.__namespace__ = types.MappingProxyType(namespace)

The practical downside of that approach is that it still doubles the storage requirements for every class that uses a custom namespace, even if the custom namespace isn't actually needed after creation. The upside is that you can write a class decorator that needs ordering information and say "to use this decorator, you must also specify 'namespace=collections.OrderedDict()'"

There's also a major complexity downside to that approach - the distinction between __dict__ and __namespace__ is somewhat subtle, especially since __namespace__ won't see later changes made through setattr() and delattr(), and __dict__ won't see changes made through any external references to the custom namespace.

That means my current inclination is to instead change the signature of __init_class__ to accept a read-only view of the execution namespace as a second parameter. Decorators won't have access to the details lost by the copying of attributes into an ordinary dict unless used with a metaclass or base class that takes steps to attach it to the created class object. I can live with that limitation, and it means we only have to properly document the "the contents of the class execution namespace are copied into an ordinary dict instance when the class is created" behaviour of type to explain why __init_class__ has the second parameter.
History
Date User Action Args
2013-03-16 13:16:54ncoghlansetrecipients: + ncoghlan, jcea, ezio.melotti, Arfrever, flox, daniel.urban, eric.snow
2013-03-16 13:16:54ncoghlansetmessageid: <1363439814.43.0.0148769670132.issue17044@psf.upfronthosting.co.za>
2013-03-16 13:16:54ncoghlanlinkissue17044 messages
2013-03-16 13:16:53ncoghlancreate