[Python-Dev] bug or a feature?

Scott Dial scott+python-dev at scottdial.com
Thu Jun 12 04:44:17 CEST 2008

Maciej Fijalkowski wrote:
> 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.
> 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.

Not withstanding Terry's respond, this is not as off-topic as you make
it out to be. The test case you cited is in fact test this exact
'feature'. And as Terry expounded on it, it was unclear to me whether
that was even of allowed. The only reason the test used locals() was
because it was the only way to insert a non-string key into the class

>>> class A:
...     locals()[42] = 98
>>> A.__dict__
{'__module__': '__main__', 43: 1, '__doc__': None}

locals() has to be used because __dict__ is unavailable at definition.

>>> class A:
...     __dict__[42] = 98
NameError: name '__dict__' is not defined

So, while one can write:

>>> class A: pass
>>> a.__dict__[42] = 98

But that's not quite the same. Nevertheless, it was still unclear
whether there was any pronouncements on non-string keys. Sorry for
wasting your time.


Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu

More information about the Python-Dev mailing list