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

Christian Heimes lists at cheimes.de
Thu Feb 5 22:04:18 CET 2009


Guido van Rossum schrieb:
> I'd prefer a compromise -- the keys should be strings, but should not
> be required to be valid identifiers. All Python implementations should
> support this. Rationale: a type check is cheap, and using strings
> exclusively makes the use of a faster dict implementation possible. A
> check for a conforming identifier is relatively expensive and serves
> no purpose except being pedantic.

As pointed out in my other posting, an additional type check isn't
required in most cases. A C function that checks ma_lookup does the same
trick.

int
PyDict_StringKeysOnly(PyObject *dict) {
    if (((PyDictObject*)dict)->ma_lookup == lookdict_string)
        return 1;
    else {
        /* check all keys for PyStringObject */
    }
}

The performance penalty is slime to nothing for the common case.

How are we going to handle str subclasses and unicode? Should we allow
all subclasses of basestring? Or just str and unicode? Or str only?

Christian


More information about the Python-Dev mailing list