【问题标题】:Getting variables in a class?在类中获取变量?
【发布时间】:2015-02-01 14:29:01
【问题描述】:

当我尝试这样做时:

print(pyfancy.RED + "Hello Red!" + pyfancy.END)

RED 和 END 是一个变量,但是当我运行它时,我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'RED'

以前有效,现在无效!

请帮忙!

【问题讨论】:

  • 什么是pyfancy?模块名还是对象名?
  • 你先import pyfancy了吗?
  • 能否提供“pyfancy”的代码?
  • @Cyber​​,他显然已经导入了 pyfancy,正如您在错误中看到的那样,python 识别出它是一个模块

标签: python class variables


【解决方案1】:

听起来您有一个名为 pyfancy 的模块,其中包含一个类,可能也称为 pyfancy(?)。

要么写pyfancy.pyfancy.RED,要么像这样导入类:

from pyfancy import pyfancy

(您可能需要调整它以匹配代码中使用的实际类名。)

【讨论】:

    【解决方案2】:

    RED 可能是名为pyfancy 的模块中名为pyfancy 的类的类属性 - 您需要from pyfancy import pyfancy 或使用pyfancy.pyfancy.RED 等。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2014-10-29
      相关资源
      最近更新 更多