Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

With your bare hands?!?


devel / comp.lang.python / Re: list indices must be integers or slices, not str

SubjectAuthor
o Re: list indices must be integers or slices, not strChris Angelico

1
Re: list indices must be integers or slices, not str

<mailman.41.1658309839.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23302&group=comp.lang.python#23302

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: list indices must be integers or slices, not str
Date: Wed, 20 Jul 2022 19:37:06 +1000
Lines: 55
Message-ID: <mailman.41.1658309839.20444.python-list@python.org>
References: <06dda811-2a19-b575-7837-e14415ea912b@chagford.com>
<CAPTjJmrG17+krCLYyQ6si4DekgvAk2PKbJAyK7tUhgYqXGg5pQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de hzCDS5HzCwyM3GVqWa3tmQLbNStASbXZLQevwTjXs0Qg==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=Qj64PHn/;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'jul': 0.04; 'traceback':
0.04; '(most': 0.05; '2022': 0.05; 'last):': 0.05; 'aug': 0.07;
'string': 0.07; 'expression': 0.09; 'lookup': 0.09; 'subject:not':
0.09; 'typeerror:': 0.09; 'subject:list': 0.11; 'backward': 0.16;
'chrisa': 0.16; 'error?': 0.16; 'expressions.': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'integer': 0.16; 'integer,': 0.16; 'millman': 0.16;
'received:209.85.218': 0.16; 'wrote:': 0.16; 'python': 0.16;
"can't": 0.17; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22;
'anyone': 0.25; 'seems': 0.26; 'skip:{ 20': 0.26; 'else': 0.27;
'bit': 0.27; '>>>': 0.28; 'seem': 0.31; "doesn't": 0.32; '"",':
0.32; 'negative': 0.32; 'words,': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'file': 0.38; 'means': 0.38;
'received:209': 0.39; 'list': 0.39; 'seeing': 0.39; 'wed,': 0.39;
'case.': 0.40; 'explain': 0.40; 'should': 0.40; 'counts': 0.60;
'best': 0.61; 'everything': 0.63; 'named': 0.65; 'interpreted':
0.69; 'sequence': 0.69; 'little': 0.73; 'documented': 0.76;
'attribute': 0.84; 'characters': 0.84; 'indices': 0.84; 'selects':
0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=EERkbtgf9TGY9ZoW2Z+rjdN6vPxvs0ObMh4BgOV6+9A=;
b=Qj64PHn/EhzZgU6mFXFFee4qvYvYmVc5rpT6hT8Wsr9mlQFcBQHDcY0ljoKbQeiwIj
dh9IHwXDnacUmKTZU9wBkYa/l7xgj/GSNfaQWJfzBxChHANWYR4AaJXFTNUtJNFiuiV7
X5tBe1FBqmpPKWJETyiKQQ8QECAcEdPzyS63wg2EA0goh9oQ9XswdTamCU090QxVesKV
JFFHUrdZFgl/aCaVHo7KlgqSFHZnVc9ERmTaDqtRwahjpglq0oeSBP1Ykfer1DtCB2ol
WvCDDnCpzCNd8+iRQ4SnFnavPPHClUlaZvNlwiXv3+inK7Dvx6atgYCjG55lLUhO59F9
RZEA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=EERkbtgf9TGY9ZoW2Z+rjdN6vPxvs0ObMh4BgOV6+9A=;
b=fCRsNELDhi4fuGs832yW0x7HXcZpXUkmWosDTrmTLgo4pOoieJumjsuvCNwWJWxNN4
Q5MA3+48U2NilJpD3kwVIp4XinezX/OBdIPtoQyErIvVxdotTYErg4xo8QI5JJWm51eG
3mz8P7FFFPW3TCR4xoFDFsDcGy4ui+1s6vRkHpIk1/o733NcZhvTsx+Hnn+9LZ7xckV4
K70UM5zrYbDyeMw3UExzWljdGYISMo496rMwXPUIIeEYV8cBmdmkzpoSY22oIZuC/0ZB
UfmZCrykQKAa7P/QBaV0SoZil++WiOMMRhjsNMXH7JFAsWeFc/HKQ4FNlN/1HOG4BnnD
d2LA==
X-Gm-Message-State: AJIora+aqZ2fmzt/ONL94P6wyMNOGYT0H+uPHfZ75gRFMF6mPqV4D4j/
SejKZaypLDDy9Smgj8pPngID1KCboD2nsUFjUXk7bCeO360=
X-Google-Smtp-Source: AGRyM1tkh8E9p5FGLe+KrS0MbJtU33zfAiGJBcCt/PMDcsMzdxbP6hs3e5zUnS/9DYGz94ksi57848D9bfdeLCRnU3w=
X-Received: by 2002:a17:907:7ba8:b0:72b:9186:5bff with SMTP id
ne40-20020a1709077ba800b0072b91865bffmr34706456ejc.650.1658309837649; Wed, 20
Jul 2022 02:37:17 -0700 (PDT)
In-Reply-To: <06dda811-2a19-b575-7837-e14415ea912b@chagford.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmrG17+krCLYyQ6si4DekgvAk2PKbJAyK7tUhgYqXGg5pQ@mail.gmail.com>
X-Mailman-Original-References: <06dda811-2a19-b575-7837-e14415ea912b@chagford.com>
 by: Chris Angelico - Wed, 20 Jul 2022 09:37 UTC

On Wed, 20 Jul 2022 at 18:34, Frank Millman <frank@chagford.com> wrote:
>
> Hi all
>
> C:\Users\E7280>python
> Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64
> bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>>
> >>> x = list(range(10))
> >>>
> >>> '{x[1]}'.format(**vars())
> '1'
> >>>
> >>> '{x[-1]}'.format(**vars())
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers or slices, not str
> >>>
>
> Can anyone explain this error? It seems that a negative index is deemed
> to be a string in this case.
>

Yeah, that does seem a little odd. What you're seeing is the same as
this phenomenon:

>>> "{x[1]} {x[spam]}".format(x={1: 42, "spam": "ham"})
'42 ham'
>>> "{x[1]} {x[spam]}".format(x={"1": 42, "spam": "ham"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1

But I can't find it documented anywhere that digits-only means
numeric. The best I can find is:

https://docs.python.org/3/library/string.html#formatstrings
"""The arg_name can be followed by any number of index or attribute
expressions. An expression of the form '.name' selects the named
attribute using getattr(), while an expression of the form '[index]'
does an index lookup using __getitem__()."""

and in the corresponding grammar:

field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
index_string ::= <any source character except "]"> +

In other words, any sequence of characters counts as an argument, as
long as it's not ambiguous. It doesn't seem to say that "all digits is
interpreted as an integer, everything else is interpreted as a
string". ISTM that a negative number should be interpreted as an
integer too, but that might be a backward compatibility break.

ChrisA


devel / comp.lang.python / Re: list indices must be integers or slices, not str

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor