Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70166 > unrolled thread

indexing in format strings

Started byblindanagram <noone@nowhere.net>
First post2014-04-11 22:33 +0100
Last post2014-04-11 22:44 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  indexing in format strings blindanagram <noone@nowhere.net> - 2014-04-11 22:33 +0100
    Re: indexing in format strings Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-11 22:43 +0100
    Re: indexing in format strings Terry Reedy <tjreedy@udel.edu> - 2014-04-11 17:45 -0400
    Re: indexing in format strings blindanagram <noone@nowhere.net> - 2014-04-12 00:13 +0100
      Re: indexing in format strings Terry Reedy <tjreedy@udel.edu> - 2014-04-11 22:44 -0400

#70166 — indexing in format strings

Fromblindanagram <noone@nowhere.net>
Date2014-04-11 22:33 +0100
Subjectindexing in format strings
Message-ID<HrednSzZyaoTwtXOnZ2dnUVZ8kydnZ2d@brightview.co.uk>
With:

   l = [1,2,3]

this:

   print('{0[0]:d}..{0[2]:d}'.format(l))

gives 1..3 but this:

   print('{0[0]:d}..{0[-1]:d}'.format(l))

gives:

   Traceback (most recent call last):
    File "<string>", line 1, in <fragment>
   builtins.TypeError: list indices must be integers, not str

which seems to me counterintuitive.

I expected indexing in a format string to behave as it does elsewhere
but this seems not to be true.

[toc] | [next] | [standalone]


#70167

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-04-11 22:43 +0100
Message-ID<mailman.9217.1397252635.18130.python-list@python.org>
In reply to#70166
On 11/04/2014 22:33, blindanagram wrote:
> With:
>
>     l = [1,2,3]
>
> this:
>
>     print('{0[0]:d}..{0[2]:d}'.format(l))
>
> gives 1..3 but this:
>
>     print('{0[0]:d}..{0[-1]:d}'.format(l))
>
> gives:
>
>     Traceback (most recent call last):
>      File "<string>", line 1, in <fragment>
>     builtins.TypeError: list indices must be integers, not str
>
> which seems to me counterintuitive.
>
> I expected indexing in a format string to behave as it does elsewhere
> but this seems not to be true.
>

See http://bugs.python.org/issue7951

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#70168

FromTerry Reedy <tjreedy@udel.edu>
Date2014-04-11 17:45 -0400
Message-ID<mailman.9218.1397252732.18130.python-list@python.org>
In reply to#70166
On 4/11/2014 5:33 PM, blindanagram wrote:
> With:
>
>     l = [1,2,3]
>
> this:
>
>     print('{0[0]:d}..{0[2]:d}'.format(l))
>
> gives 1..3 but this:
>
>     print('{0[0]:d}..{0[-1]:d}'.format(l))
>
> gives:
>
>     Traceback (most recent call last):
>      File "<string>", line 1, in <fragment>
>     builtins.TypeError: list indices must be integers, not str
>
> which seems to me counterintuitive.
>
> I expected indexing in a format string to behave as it does elsewhere
> but this seems not to be true.

Been discussed on the tracker. Consider:

 >>> '{0[-1]}'.format({'-1': 'neg int key'})
'neg int key'

Not quoting string keys within the format string introduces ambiguity 
settled by 'string unless all digits, then int'.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#70170

Fromblindanagram <noone@nowhere.net>
Date2014-04-12 00:13 +0100
Message-ID<ieGdnV5uV5-06tXOnZ2dnUVZ7vWdnZ2d@brightview.co.uk>
In reply to#70166
On 11/04/2014 22:33, blindanagram wrote:

Thanks, Mark and Terry, for your rapid responses.

An interesting thread.

   Brian

[toc] | [prev] | [next] | [standalone]


#70175

FromTerry Reedy <tjreedy@udel.edu>
Date2014-04-11 22:44 -0400
Message-ID<mailman.9221.1397270674.18130.python-list@python.org>
In reply to#70170
On 4/11/2014 7:13 PM, blindanagram wrote:
> On 11/04/2014 22:33, blindanagram wrote:
>
> Thanks, Mark and Terry, for your rapid responses.
>
> An interesting thread.

It just occurred to me today, and I verified, that '+1' is also seen as 
a string.

 >>> '{0[-1]}'.format({'-1': 'neg int key'})
'neg int key'
 >>> '{0[+1]}'.format({'+1': 'neg int key'})
'neg int key'
 >>> '{0[+1]}'.format([1,2,3])
Traceback (most recent call last):
   File "<pyshell#16>", line 1, in <module>
     '{0[+1]}'.format([1,2,3])
TypeError: list indices must be integers, not str


-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web