[DB-SIG] pyformat Parameter Style

Chris Cogdon chris at cogdon.org
Tue May 13 19:29:26 EDT 2003

On Tuesday, May 13, 2003, at 09:57 US/Pacific, Gerhard Häring wrote:

> I was going to answer in this thread, but there's nothing I could add 
> to this brilliant post.

Thanks for the support :) Let me add a little more fuel to the fire...

Chris Cogdon wrote:
> cursor.execute ( "insert into data ( %2.2f )", my_float )
> Now, why in the world would you ever WANT to strip off the precision 
> from the value? It just doesn't make any sense.

In addition to that, stripping the precision in such a manner CHANGES 
THE VALUE of the data being passed to the backend, which is beyond the 
scope of what the % operator was intended for. If one really wanted to 
change the value in such a manner, I would recommend something like the 

     cursor.execute ( "insert into data ( %s )", round(my_float,2) )

which is a lot more obvious what's going on. Another example....

     cursor.execute ( "insert into data ( %r )", my_anything )

What in the world does THIS mean?! Currently, the backend code, on 
receiving a string, will make the 'appropriate' quoting changes to the 
string so it can be passed to the backend (this is why you don't put 
quotes around the %s; the DBI does it for you). For many DBMSes, the 
quoting rules are a little different (like, how to handle embedded 
special characters), or VERY different (like what type of quoting marks 
are allowed) than python. If we allow %r into the mix, what does this 
actually mean? Do we do the standard python repr() conversion, then 
pass it to the DB's quoting rules, or the other way around, or just one 
of them? (which won't work in a lot of cases).

This is becoming a real mess.

The ONLY valid solution I see is to very specifically state that 
pyformat allows %s and %(keyname)s, and nothing else, which is what 
many API's already do; they don't specifically check for %non-s, but it 
doesn't work, because the parameters are already strings by the time it 
gets to the python % operator (see code snippets previous in this 

The docs also imply that 'format' is for '%s' and 'pyformat' is for 
'%(keyname)s', but many APIs (eg, pyPgSQL) allow both, and 
intelligently detect.

    ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.\  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

More information about the DB-SIG mailing list