[Python-3000] Issues with PEP 3101 (string formatting)

Talin talin at acm.org
Sun Jun 24 08:01:17 CEST 2007


Chris McDonough wrote:
> Allowing attribute and/or item access within templating expressions  
> has historically been the domain of full-on templating languages  
> (which invariably also have a way to do repeats, conditionals,  
> arbitrary method calls, etc).
> 
> I think it should probably stay that way because to me, at least,  
> there's not much more compelling about being able to do item/ 
> attribute access within a template expression than there is to be  
> able to do replacements using results from arbitrary method calls.   
> It's fairly arbitrary to allow calls to __getitem__ and __getattr__  
> and but prevent, say, calls to "traverse", at least if the format  
> arguments are not restricted to plain lists/tuples/dicts.

I don't buy this argument - in that I don't think its arbitrary. You are 
correct that 3101 is not intended to be a full-on templating language, 
but that doesn't mean that we can't extend it beyond what, say, printf 
can do.

The current design is a mid-point between Perl's interpolated strings 
(which can contain arbitrary expressions), and C-style printf. The 
guiding rule is to allow expressions which increase convenience and 
expressiveness, and which are likely to be useful, while disallowing 
most of the types of expressions which would be likely to have side 
effects. Since this is Python, we can't guarantee that there's no side 
effects, but we can make a pretty good guess based on the assumption 
that most Python programmers are reasonable and sane.

 From an implementation standpoint, this is not where the complexity 
lies. (The most complex part of the code is the part dealing with 
details of conversion specifiers and formatting of numbers.)

> That's not to say that maybe an extended templating thingy shouldn't  
> ship within the stdlib though, maybe even one that extends the  
> default interpolation syntax in these sorts of ways.
> 
> - C
> 
> On Jun 20, 2007, at 10:49 AM, Nick Coghlan wrote:
> 
>> Chris McDonough wrote:
>>> Wrt http://www.python.org/dev/peps/pep-3101/
>>> PEP 3101 says Py3K should allow item and attribute access syntax   
>>> within string templating expressions but "to limit potential  
>>> security  issues", access to underscore prefixed names within  
>>> attribute/item  access expressions will be disallowed.
>> Personally, I'd be fine with leaving at least the embedded  
>> attribute access out of the initial implementation of the PEP. I'd  
>> even be OK with leaving out the embedded item access, but if we  
>> leave it in "vars(obj)" and the embedded item access would still  
>> provide a shorthand notation for access to instance variable  
>> attributes in a format string.
>>
>> So +1 for leaving out embedded attribute access from the initial  
>> implementation of PEP 3101, and -0 for leaving out the embedded  
>> item access.
>>
>> Cheers,
>> Nick.
>>
>> -- 
>> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>> ---------------------------------------------------------------
>>             http://www.boredomandlaziness.org
>>
> 
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/talin%40acm.org
> 


More information about the Python-3000 mailing list