为什么在试图以相同的逻辑方式访问不存在的索引时,“索引”的形状不同?

发布于 2025-01-27 15:59:59 字数 549 浏览 4 评论 0原文

当调试一些我仍然不了解的错误时,我偶然发现了一件怪异的事情。

当我以两个逻辑上相似的方式访问列表,但通过代码不同的方式,以超出范围的索引,我在Pycharm的评估窗口中遇到了两个略有不同的错误。

看起来这样:

定义

a = [1, 2, 3]

然后

1)

a[111]

结果:

{IndexError}list index out of range
t = 111
a[t]

结果:

IndexError: list index out of range

当我在每个访问之前添加列表定义时,错误看起来与第二版相同。

它有意义吗?从中可以理解什么?

谢谢!

I have stumbled upon a weird (to me) thing when debugging some bug I still have not understood.

When I access a list in two logically similar, but code-wise different ways, in an index that is out of range, I get two slightly different errors in the Evaluation window in pycharm.

Looks like this:

definition:

a = [1, 2, 3]

then:

1)

a[111]

Result:

{IndexError}list index out of range
t = 111
a[t]

Result:

IndexError: list index out of range

When I add the list definition right before each access, the error looks the same, as the second version.

Does it have any significance? What can be understood from this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文