[Python-Dev] Non-string keys in type dict
PJ Eby
pje at telecommunity.com
Thu Mar 8 18:16:28 CET 2012
More information about the Python-Dev mailing list
Thu Mar 8 18:16:28 CET 2012
- Previous message: [Python-Dev] Request for clarification of PEP 380
- Next message: [Python-Dev] PEP
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Mar 8, 2012 at 2:43 AM, Ethan Furman <ethan at stoneleaf.us> wrote: > > PJ Eby wrote: >> >> Short version: AddOns are things you can use to dynamically extend instances -- a bit like the "decorator" in "decorator pattern" (not to be confused with Python decorators). Rather than synthesize a unique string as a dictionary key, I just used the AddOn classes themselves as keys. This works fine for object instances, but gets hairy once classes come into play. > > > Are you able to modify classes after class creation in Python 3? Without using a metaclass? For ClassAddOns, it really doesn't matter; you can't remove them from the class they attach to. Addons created after the class is finalized use a weakref dictionary to attach to their classes. Now that I've gone back and looked at the code, the only reason that ClassAddOns even use the class __dict__ in the first place is because it's a convenient place to put them while the class is being built. With only slightly hairier code, I could use an __addons__ dict in the class namespace while it's being built, but there'll then be a performance hit at look up time to do cls.__dict__['__addons__'][key] instead of cls.__dict__[key]. Actually, now that I'm thinking about it, the non-modifiability of class dictionaries is actually a feature for this use case: if I make an __addons__ dict, that dict is mutable. That means I'll have to move to string keys or have some sort of immutable dict type available... ;-) (Either that, or do some other, more complex refactoring.) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120308/d9c3fff2/attachment.html>
- Previous message: [Python-Dev] Request for clarification of PEP 380
- Next message: [Python-Dev] PEP
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list