【问题标题】:Getting a TypeError (tuple assignment) in one interpreter, and not the other在一个解释器中获取 TypeError(元组分配),而不是另一个
【发布时间】:2019-02-18 18:05:00
【问题描述】:

我遇到了一个无法解释的奇怪问题。我有一些我在笔记本电脑上用 PyCharms 编写的代码,并对其进行了非常彻底的测试(我想)。我现在已经迁移到 Ubuntu 环境,设置了我的 virtualenv,安装了我的模块,并开始测试......

在 PyCharms 和 Ubuntu 中进行相同的测试,我得到两个单独的结果,我无法解释。我需要 Ubuntu 版本才能工作,但是当 PyCharms 和那里的解释器一直不在乎时,我不明白为什么它不喜欢我的元组分配。

Ubuntu 环境:使用 Python 3.6.5 Windows 10 环境:使用 Python 3.6.5(通过 Python 下载)

我的脚本在这里失败:

if something[7] is None:
    something[7] = "N/A"

来自 Ubuntu 的错误:

[2018-09-13 19:49:28,706] ERROR [__main__.main:445] Problem occurred. Please see log!
  Traceback (most recent call last):
    File "script.py", line 320, in main
      something[7] = "N/A"
TypeError: 'tuple' object does not support item assignment

这是 PyCharm 的解释器在没有问题的情况下修改的实际元组。我可以在调试中看到这一点:

('Val', 'Val', 'Val', 'Val', 'Val', 'Val', 'Val', 'N/A', 'Val', None, None, None, None, None, None)

有什么想法吗?

【问题讨论】:

  • 错误说明了一切。元组元素不能更改。您需要将something 设为列表或其他对象。不过,我看不出它在其他地方是如何起作用的。

标签: python-3.x ubuntu pycharm tuples ubuntu-16.04


【解决方案1】:

您不能将项目分配给元组,但是,您可以附加到元组。元组是不可变的。

有关元组的更多详细信息,请参阅Python Documentation。对于您的用例,list 似乎是更好的数据类型。

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2021-08-06
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多