[Python-Dev] quick poll: could int, str, tuple etc. become type objects?

Fredrik Lundh fredrik@pythonware.com
Wed, 6 Jun 2001 00:57:43 +0200


greg wrote:
> > - What should the argument to dict() be?  A list of (key, value)
> >   pairs, a list of alternating keys and values, or something else?
>
> I love /F's suggestion
>
>   dict(k=v, k=v, ...)
>
> but that's icing on the cake -- cool feature, looks pretty, etc.

note that the python interpreter builds that dictionary for
you if you use the METH_KEYWORDS flag...

> I think the real answer should be
>
>   dict(k, v, k, v)
>
> like Jython.

given that Jython already gives a meaning to dict with more
than one argument, I suggest:

    dict(d) # consistency
    dict(k, v, k, v, ...) # jython compatibility
    dict(*[k, v, k, v, ...]) # convenience
    dict(k=v, k=v, ...) # common pydiom

and maybe:

    dict(d.items()) # symmetry

> If both can be supported, that would be swell.

how about:

    if (PyTuple_GET_SIZE(args)) {
        assert PyDict_GET_SIZE(kw) == 0
        if (PyTuple_GET_SIZE(args) == 1) {
            args = PyTuple_GET_ITEM(args, 0);
            if (PyDict_Check(args))
                dict = args.copy()
            else if (PySequence_Check(args))
                dict = {}
                for k, v in args:
                    dict[k] = v
        } else {
            assert (PySequence_Size(args) & 0) == 0 # maybe
            dict = {}
            for i in range(len(args)):
                dict[args[i]] = args[i+1]
        }
    } else {
        assert PyDict_GET_SIZE(kw) > 0 # probably
        dict = kw
    }