[Python-Dev] bug or a feature? (original) (raw)

Maciej Fijalkowski fijall at gmail.com
Thu Jun 12 02:59:27 CEST 2008


On Thu, Jun 12, 2008 at 2:32 AM, Terry Reedy <tjreedy at udel.edu> wrote:

"Scott Dial" <scott+python-dev at scottdial.com> wrote in message news:4850263A.3040805 at scottdial.com... || If non-string keys are not allowed in dict, then the AddOns library | should be changed to add another dict to the object of interest to track | these AddOn instances. There are three possibilities with respect to dict and non-string keys. 1. All implementations must reject such. 2. Each implementation decides for itself. 3. All implementations must allow such. Current, CPython does not reject, eliminating 1). Since, as I understand it, at least 1 other implementation does reject, 3) is also eliminated until Guido decrees otherwise and such implementation(s) change. This leaves 2) as the de facto situation, but this could be made clearer in the docs: "The result of trying to add non-string keys to any dict attribute is implementation defined." This means that portable Python code must act as if 1) were the case. The Data Model chapter of the Reference Manual lists .dict as a special attribute of callables, modules, classes, and instances. It describes dict as a "namespace dictionary" or "implementation of the namespace" thereof. Since namespaces map names (or possibly non-name strings) to objects, this to me implies that an implementation is and should not be required to allow non-strings in dict. The same chapter has more than one sentence saying something like "o.x is equivalent to o.dict['x']". While one could read this as prohibiting o.dict[nonstring], one could also read it as being silent, neither allowing nor prohibiting. The builtin interface functions setattr, hasattr, getattr all require strings for accessing the underlying dict. Since they could have been defined otherwise, to accept any object as an attribute 'name' (key), this _again implies (to me) that dict_s are only intended and only required to have string keys. Hence, I was initially surprised that 1) above was not true. So I might add something stronger to the docs, something like "The special dict attributes are only intended to hold string keys. If an implementation allows other keys, that is only an current accidental side-effect of considerations of parsimony and efficiency." Contrariwise, if 3) were mandated, then I would think that the xxxattr functions should be changed. Terry Jan Reedy


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fijall%40gmail.com

This is completely irrelevant. This post is not about assigning non-string stuff to dict of class which works completely fine. It's about abusing locals, which are not even given that they'll modify this dict.



More information about the Python-Dev mailing list