Germán L. Osella Massa <[email protected]> added the comment:
I finally managed to get the time to finish the patch that allows negative
indexes inside square brackets so now they work with the same semantics as in a
python expression:
>>> '{0[-1]}'.format(['abc', 'def'])
'def'
>>> '{0[-2]}'.format(['abc', 'def'])
'abc'
>>> '{0[-1][-1]}'.format(['abc', ['def']])
'def'
They work auto-numbered fields too:
>>> '{[-1]}'.format(['abc', 'def'])
'def'
Also, a positive sign is now accepted as part of a valid integer:
>>> '{0[+1]}'.format(['abc', 'def'])
'def'
As a bonus, negatives indexes are also allowed to refer to positional arguments:
>>> '{-1}'.format('abc', 'def')
'def'
>>> '{-2}'.format('abc', 'def')
'abc'
I'm attaching a patch against trunk. I added some tests for this functionality
in test_str.py.
By the way, this code doesn't work anymore:
>>> "{[-1]}".format({'-1': 'X'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: -1L
But now it behaves in the same way as:
>>> "{[1]}".format({'1': 'X'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1L
I didn't attempt to ignore whitespaces when trying to parse the index as an
integer (to allow that "{ 0 }" can be treated as "{0}" and "{0[1]}" as "{ 0 [ 1
] }") because I'm not sure if this behavior is desirable.
----------
keywords: +patch
Added file: http://bugs.python.org/file17712/format_negative_indexes-2.7.diff
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue7951>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com