【问题标题】:Passing a variable through a class Python通过类 Python 传递变量
【发布时间】:2021-07-08 04:55:28
【问题描述】:

我正在尝试打印以将endpoint 变量传递给RUN() 类中的__init__ 函数。但是它给了我一个错误。 AttributeError: module 'configs' has no attribute 'endpoint'。端点文档保存在file1.py 中,并在file2.py 中调用。我如何能够将端点作为参数传递。

文件1

endpoint = 'hello'

文件2

import file1
class  RUN():
    def __init__(self, endpoint= file1.endpoint):
    
        print("Initialized")

run_websocket = RUN() 

【问题讨论】:

  • 您的代码对我来说很好用。文件是否在同一个文件夹中?
  • 你能分享整个file1.py
  • 最好创建一个名为 config 或 cfg 的模块以使该模块全局可用。
  • 您是否有可能在代码中的某处使用configs.endpoint 或类似的东西?

标签: python function file class error-handling


【解决方案1】:

代码绝对正确。尝试删除 IDE 的缓存或 build 和 dist 文件夹等,然后再次运行。

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2012-05-26
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2014-12-25
    • 2015-06-23
    相关资源
    最近更新 更多