【问题标题】:AttributeError: 'unicode' object has no attribute 'add' - Operator ModuleAttributeError: 'unicode' 对象没有属性 'add' - 操作员模块
【发布时间】:2015-07-16 20:14:00
【问题描述】:

我有一本字典,我正在使用操作员模块来执行此操作。这是它的样子:

           ops =  {"+": operator.add,
                   "-": operator.sub,
                   "*": operator.mul,
                   "/": operator.div}

但是,当我运行程序时出现错误:

AttributeError: 'unicode' object has no attribute 'add'

我该如何解决这个问题?

【问题讨论】:

  • 发布您的实际代码。您在没有任何参数的情况下调用 operator.add(),但这不会引发您在问题中发布的错误。
  • 另外,请阅读:mcve
  • @AshwiniChaudhary 我添加了 () 作为一个错误,我相应地更新了
  • 您可能有一个名为 operator 的局部变量覆盖了导入。
  • @karthikr,我看到我确实有一个覆盖导入的局部变量 operator

标签: python operator-keyword


【解决方案1】:

我有一个名为 operator 的局部变量,它覆盖了模块的导入。

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2021-12-07
    • 2015-06-24
    • 2017-07-19
    • 2010-11-18
    相关资源
    最近更新 更多