【问题标题】:How to pass argument from a class to another file如何将参数从一个类传递到另一个文件
【发布时间】:2021-04-24 08:15:45
【问题描述】:

我正在尝试将参数“a”从 file_a 传递给 file_b。那么如何在 file_b 中获取此变量

(page file_a.py)
class try():
    def pass_this():
        a = 7
(page file_b.py)
from file_a import try

如果有人可以解释我是否可以在不使用课程的情况下做到这一点(如果可能的话),它将帮助我

【问题讨论】:

  • Python 的基本组织是基于模块和包,而不是文件。

标签: python import python-class


【解决方案1】:

file_a.py 您可以创建一个名为 a 的变量 a = 7

file_b.py 中,导入这个变量,如:

from file_a import a

如果‘a’是类属性或在某些函数或方法中使用全局关键字

def myFunc():
       global a
        a = 7 

【讨论】:

    【解决方案2】:

    您可以将 a 设置为全局吗?

    (page file_a.py)
    a = 7
    
    (page file_b.py)
    from file_a import a
    

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2018-03-27
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2017-01-23
      相关资源
      最近更新 更多