[Python-Dev] The interpreter accepts f(**{'5':'foo'}); is this intentional?

Christian Heimes lists at cheimes.de
Thu Feb 5 21:36:04 CET 2009


Nick Coghlan wrote:
> Generally speaking, Python namespace dictionaries (be it globals(),
> locals(), the __dict__ attribute of an instance or a set of keyword
> arguments) aren't required to enforce the use of legal identifiers (in
> many cases, the CPython variants don't even enforce the use of strings).

Side note:

CPython's dict code has a special case for str objects (PyStringObject
in 2.x, PyUnicodeObject in 3.x). The internal lookup method is optimized
for str objects. Python uses dict objects for all its namespaces like
classes, modules and most objects, so dict with str as keys are pretty
common.

The first time a non str object is inserted or looked up, the dict
swiches to a more general lookup methods. lookdict() still fast but not
as fast as lookdict_string(). It doesn't make a huge difference but you
should still keep the fact in your head.

We could abuse the state of the ma_lookup function pointer to check the
dict for str only keys. But it would break for unicode keys thus making
from __future__ import unicode_literals useless.

Christian



More information about the Python-Dev mailing list