Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

A mathematician is a device for turning coffee into theorems. -- P. Erdos


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 strRoel Schroeven

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: roel@roelschroeven.net (Roel Schroeven)
Newsgroups: comp.lang.python
Subject: Re: list indices must be integers or slices, not str
Date: Wed, 20 Jul 2022 22:59:21 +0200
Lines: 43
Message-ID: <mailman.56.1658350767.20444.python-list@python.org>
References: <06dda811-2a19-b575-7837-e14415ea912b@chagford.com>
<CAPTjJmrG17+krCLYyQ6si4DekgvAk2PKbJAyK7tUhgYqXGg5pQ@mail.gmail.com>
<0d6e1849-1e4d-3587-7dc6-748e1b8ce17c@chagford.com>
<6ad50524-a639-ed31-0d1d-17ac8e402ed2@chagford.com>
<32cf1828-a074-6d5c-5f60-ab9f5b880a8a@roelschroeven.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 0GljpACV4iLc5MKehogkMwO+H03qm7Bp0/z0JgyNwagg==
Return-Path: <roel@roelschroeven.net>
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=roelschroeven.net header.i=@roelschroeven.net
header.b=cUhlREc2; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'jul': 0.04; 'traceback':
0.04; '"it': 0.05; '(most': 0.05; '2022': 0.05; 'last):': 0.05;
"python's": 0.05; 'string': 0.07; 'template': 0.07; 'attract':
0.09; 'subject:not': 0.09; 'typeerror:': 0.09; 'subject:list':
0.11; '"there': 0.16; 'dict': 0.16; 'engine,': 0.16; 'error?':
0.16; 'for.': 0.16; 'integer': 0.16; 'mechanisms': 0.16;
'millman': 0.16; 'object,': 0.16; 'received:10.202': 0.16;
'received:10.202.2': 0.16; 'received:10.202.2.163': 0.16;
'received:internal': 0.16; 'received:messagingengine.com': 0.16;
'schreef': 0.16; 'symbol': 0.16; 'wrote:': 0.16; "can't": 0.17;
'to:addr:python-list': 0.20; 'option': 0.20; "i've": 0.22;
'maybe': 0.22; 'anyone': 0.25; 'seems': 0.26; 'bit': 0.27; '>>>':
0.28; 'sense': 0.28; 'header:User-Agent:1': 0.30; 'think': 0.32;
'"",': 0.32; 'negative': 0.32; 'header:In-Reply-To:1': 0.34;
'received:66': 0.35; 'using': 0.37; 'file': 0.38; 'list': 0.39;
'wed,': 0.39; 'case.': 0.40; 'explain': 0.40; 'want': 0.40;
'should': 0.40; 'days': 0.62; 'pass': 0.64; 'complete': 0.64;
'full': 0.64; 'free': 0.72; 'formatting': 0.76; 'indices': 0.84;
'method,': 0.84; 'tables': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
roelschroeven.net; h=cc:content-transfer-encoding:content-type
:date:date:from:from:in-reply-to:in-reply-to:message-id
:mime-version:references:reply-to:sender:subject:subject:to:to;
s=fm2; t=1658350763; x=1658437163; bh=igDX03w5ARtjWxcuWvjGW3Pq/
xIL7LTBXbntlTgCLMg=; b=cUhlREc2j+K9Z7wbB0xDG6959cVL1jtIjgWuhA6LX
qKp/SEOi3hJkBUePAv/D1W3Rea7/JW5Hv1TBblpm/bLO3sXrPwhO957aNVDxA6XB
eA+BcK3QlsBYeY9M1MznJ3OUuvK7Ng1rqU0mbXFpnf80lz2GvfPmPmz8x8ad0bVY
8N7mWTyG0O2sBcsTWZE5Lqv0x5iWCqLM5Dl36W/J/qyWAGaUG7IhzcYOPQ/XMVWB
z7jNFIw5l4KwLThPAii70Ek6EhDljg1DZw11Bvl0Gpz4/+NdtT29nC9YTgeqyvpu
9zfU5xIMhf13t1qXGqFhqvIyD1VXPEu2dQcZhtL8nmCVw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
messagingengine.com; h=cc:content-transfer-encoding:content-type
:date:date:feedback-id:feedback-id:from:from:in-reply-to
:in-reply-to:message-id:mime-version:references:reply-to:sender
:subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender
:x-me-sender:x-sasl-enc; s=fm3; t=1658350763; x=1658437163; bh=i
gDX03w5ARtjWxcuWvjGW3Pq/xIL7LTBXbntlTgCLMg=; b=IxacL2rAKflpXU+MQ
h9eqtMk67tQRFbwX9bDcM47qnvMN8bClH4I1Pp4u7li0XDYkLmpcBYRCkvPIsqm/
UTqWbg8LpURNqraxacnafHB+ktCx9UP7m1FPyP29+nyZZb7smi67g8WMKIwWT51j
OzO5WVxVpXG+cXShs8/3w5uVQUIlLsAqGDGLvy+5hzRIwbzdj5DJ74XZTuNapTsY
pyM62nIjO46kIGorF4hHeXmKtYgqDfeCaTYFLhYOvPbj/OOEhen3lBwZIVFb2Lsh
E3oaGWArGymIuoW8IbMj7Ke1Rfo3K0JiyZkqN98kAdTofYgz6GoVcoq8VwZE2kLz
auDNA==
X-ME-Sender: <xms:q2zYYqktyLqsYqdyLfEeIWI_pirAcBw7ZDaoVjCKkQmGQmbO1T2Y5Q>
<xme:q2zYYh1jA06-FRqbcNn9wG7BE5xAE7UA3ctPn_9gr7dlKMY7TWj-3r1kfZS5Y-5yC
Z6Mg3vQ1E15>
X-ME-Received: <xmr:q2zYYoqZmb4o8bTNNsU6wVPjYJg6IFDQP2KRmk_mcGHryAlDeZxujaldSnxJDbS-KPszRW-Bk-E2bVrxzz7xP9ru8EeFSDk>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudelvddgudehkecutefuodetggdotefrod
ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh
necuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesth
ekredttdefjeenucfhrhhomheptfhovghlucfutghhrhhovghvvghnuceorhhovghlsehr
ohgvlhhstghhrhhovghvvghnrdhnvghtqeenucggtffrrghtthgvrhhnpedttdfgieeihf
ehheffffdttdfggedvgeelgeefvdeihfektdffvdegfefhhfdtgfenucevlhhushhtvghr
ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgvlhesrhhovghlshgthh
hrohgvvhgvnhdrnhgvth
X-ME-Proxy: <xmx:q2zYYunHc6NUFLhv2hF9urinn3sf_EmUYqBTcijrLOucPXXnj3B61A>
<xmx:q2zYYo049zLCR9V6Lh9OTTTFP9lPg8CjsyNPHpJQIhPDDjWQSYOngQ>
<xmx:q2zYYlte9LjTk8dk_UXCuwVpTHCLCyzWitVwRrvXaJ9FquC_Jzkn0Q>
<xmx:q2zYYpj88-4WV9cHUDuOMbL_g7WasEHAgC6rahQ7KNxjvTkGAQcqsQ>
Feedback-ID: i8e5b41ae:Fastmail
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.0.2
Content-Language: nl, en-US
In-Reply-To: <6ad50524-a639-ed31-0d1d-17ac8e402ed2@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: <32cf1828-a074-6d5c-5f60-ab9f5b880a8a@roelschroeven.net>
X-Mailman-Original-References: <06dda811-2a19-b575-7837-e14415ea912b@chagford.com>
<CAPTjJmrG17+krCLYyQ6si4DekgvAk2PKbJAyK7tUhgYqXGg5pQ@mail.gmail.com>
<0d6e1849-1e4d-3587-7dc6-748e1b8ce17c@chagford.com>
<6ad50524-a639-ed31-0d1d-17ac8e402ed2@chagford.com>
 by: Roel Schroeven - Wed, 20 Jul 2022 20:59 UTC

Frank Millman schreef op 20/07/2022 om 13:04:
> >> On Wed, 20 Jul 2022 at 18:34, Frank Millman <frank@chagford.com> wrote:
> >>>   >>>
> >>>   >>> 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.
> >>>
> >>
> >> [...]
> >>
>
> "It seems to only want integer constants. x[2+2] and x[k] where k=2
> don't work either.
>
> I think the preferred style these days is f'{x[-1]}' which works."
>
> Unfortunately the 'f' option does not work for me in this case, as I am
> using a string object, not a string literal.
>
I've always found a bit dirty to pass indiscriminate symbol tables like
the one from globals() or vars() or vars(some_object) to a formatting
method, leaving the formatting string free to access any of it. I prefer
to make things more explicit. Can't you make a dict with a selection of
things you possible want to format?

In a sense I think you're looking for a more complete templating
solution than what Python's formatting mechanisms are designed for.
Maybe you should consider a full template engine, like Jinja?

--
"There is no cause so noble that it will not attract fuggheads."
-- Larry Niven


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