Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] A new dictionary implementation

martin at v.loewis.demartin at v.loewis.de
Wed Feb 1 21:32:36 CET 2012


> Hey, I like this! It's a subtle encouragement for developers to> initialize all their instance variables in their __init__ or __new__> method, with a (modest) performance improvement for a carrot. (Though> I have to admit I have no idea how you do it. Wouldn't the set of dict> keys be different while __init__ is in the middle of setting the> instance variables?)The "type's attribute set" will be a superset of the instance's, fora shared key set. Initializing the first instance grows the key set,which is put into the type. Subsequent instances start out with thekey set as a candidate, and have all values set to NULL in the dictvalues set. As long as you are only setting attributes that are in theshared key set, the values just get set. When it encounters a key notin the shared key set, the dict dissociates itself from the shared keyset.> Another question: a common pattern is to use (immutable) class> variables as default values for instance variables, and only set the> instance variables once they need to be different. Does such a class> benefit from your improvement?It depends. IIUC, if the first instance happens to get this attributeset, it ends up in the shared key set, and subsequent instances may havea NULL value for the key.I'm unsure how *exactly* the key set gets frozen. You cannot allow resizingthe key set once it is shared, as you would have to find all instances withthe same key set and resize their values. It would be possible (IIUC) toadd more keys to the shared key set if that doesn't cause a resize, but I'mnot sure whether the patch does that.Regards,Martin


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp