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

Terry Reedy tjreedy at udel.edu
Thu Feb 5 22:13:36 CET 2009


Christian Heimes wrote:
> 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.

This makes adding a string-only dict pretty trivial, if desired.

> 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.

Assuming that 3.x dicts are optimized for the 3.x string type, this is 
not a problem for 3.x ;-).

tjr



More information about the Python-Dev mailing list