[Python-3000] Open Issues for string.format PEP

Talin talin at acm.org
Wed Apr 26 10:37:35 CEST 2006


Ian Bicking <ianb <at> colorstudy.com> writes:


> > I want to go back for a moment to the original question. Whenever someone
asks
> > me "how should we solve this problem?" the first thing I always ask is "do
we
> > really need to?" Once we've answered that, we can then go on to the issue
of
> > coming up with a solution.
> 
> Without this all dictionaries must be copied.  I think maybe Guido 
> suggested something involving a shortcut that would not covert the 
> dictionary, but that shortcut isn't available to Python so the behavior 
> could be different for custom formatters implemented in Python.  Also, 
> only dictionaries are allowed after **; that could be changed (maybe 
> that's part of the signature changes?), but that doesn't change the fact 
> that only a dictionary can be passed into a function with **.

I think you are missing some alternatives. You see, the question I was asking
wasn't "How important is it that we be able to efficiently pass whole
dictionaries to the format function?" The question was "How important is it to
be able to pass whole dictionaries at all?" Remember, that anything that you
can do with **args you can also do by explicitly passing the individual args
you want.

Another alternative is just to fix the efficiency problems with **args. The
reason that **args is so expensive is because of the need to funnel all calls
through the existing C API, which requires that all function calls have the
signature of ( PyTuple *args, PyDict *kwargs ). If you were somehow able to
bypass or extend the C API, you could take the **args from the function call,
and directly hook it up to the **args in the formal parameter list, without
any flattening.

Assumming that neither of those options are available, I would probably then
do one of the following:

1) Create a different method name for the dict case:

    "{name}".vformat( mydict )

2) Have some sort of reserved, keyword-only argument for dicts:

    "{name}".format( indirect_args=mydict )

3) Bring back field expressions:

    "{0.name}".format( mydict )

(Perhaps the '.' operator can mean both '.' and '[]' depending on the type of
the object.)

-- Talin




More information about the Python-3000 mailing list