Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

March 22, 2004

String Substitution in Python 2.4

Prompted by AMK I took a quick look at the schedule for Python 2.4.

The features that look interesting to me are the decimal data type and, more immediately, simpler string subsitution. This proposes adding a new way of substituting a sequence of letters within a string, using '$' identifiers within the string which are replaced by the contents of a mapping or by objects from the local or global context. My first thoughts when looking at this was "When can I get this in a database module?"

Simple string substitution would make my code a lot cleaner and easier to read. It would be a boon for new users as well, given the recurring confusion about variable substitution on the db-sig mailing list.

Of course, it shouldn't work for non-string substitutions such as numeric ID values or dates. But this issue is currently glossed over in most database modules and they tend to accept '%s' simply as a placeholder, not as an indicator of string substitution. At least those that use the 'format' and 'pyformat' paramstyles (as defined in the DB-API) generally use 's' after the '%' regardless of the type of the argument you pass in and map the python data types to the database data types for you.

Perhaps the next version of the DB-API would benefit from a new paramstyle (perhaps called 'simple') which could follow the conventions of PEP 292 with the addition of support for more data types than just strings and mapping to native database data types.

Posted by Andy Todd at March 22, 2004 08:43 AM

Comments

The biggie for me is Generator Expressions[1].

[1] http://www.brunningonline.net/simon/blog/archives/001025.html

Posted by: Simon Brunning on March 23, 2004 11:52 AM