[Python-3000] Updating PEP 3101

Talin talin at acm.org
Thu May 31 08:37:07 CEST 2007


I'm in the process of updating PEP 3101 to incorporate all of the 
various discussions and evolutions that have taken place, and this is 
turning out to be fairly involved, as there are a lot of ideas scattered 
all over the place.

One thing I'd like to do is simplify the PEP a little bit, but at the 
same time address some of the requests that folks have asked for.

The goal here is to keep the basic "string.format" interface as simple 
as possible, but at the same time to allow access to more complex 
formatting for people who need it. My assumption is that people who need 
that more complex formatting would be willing to give up some of the 
syntactical convenience of the simple "string.format" style of formatting.

So for example, one thing that has been asked for is the ability to pass 
in a whole dictionary as a single argument, without using **kwds-style 
keyword parameter expansion (which is inefficient if the dictionary is 
large and only a few entries are being referred to in the format string.)

The most recent proposals have this implemented by a special 'namespace' 
argument to the format function. However, I don't like the idea of 
having certain arguments with 'special' names.

Instead, what I'd like to do is define a "Formatter" class that takes a 
larger number of options and parameters than the normal string.format 
method. People who need the extra power can construct an instance of 
Formatter (or subclass it if needed) and use that.

So for example, for people who want to be able to directly access local 
variables in a format string, you might be able to say something like:

    a = 1
    print(Formatter(locals()).format("The value of a is {a}"))

Where the "Formatter" constructor looks like:

    Formatter(namespace={}, flags=None)

In the case where you want direct access to global variables, you can 
make it even more convenient by caching the Formatter:

     f = Formatter(globals()).format
     a = 1
     print(f("The value of a is {a}"))

(You can't do this with locals() because you can't keep the dict around.)

My question to the groupmind out there is: Do you find this extra syntax 
too inconvenient and wordy, or does it seem acceptable?

-- Talin


More information about the Python-3000 mailing list