Bas*_*asj 2 python global-variables
我有
# file1.py
global a, b, c, d, e, f, g
def useful1():
# a lot of things that uses a, b, c, d, e, f, g
def useful2():
useful1()
# a lot of things that uses a, b, c, d, e, f, g
def bonjour():
# a lot of things that uses a, b, c, d, e, f, g
useful2()
Run Code Online (Sandbox Code Playgroud)
和
# main.py (main file)
import file1
# here I would like to set the value of a, b, c, d, e, f, g of file1 !
bonjour()
Run Code Online (Sandbox Code Playgroud)
如何处理所有这些全局变量?
PS:我不想a, b, c, d, e, f, g为所有功能添加,这将是非常多余的:
def useful1(a, b, c, d, e, f, g):
...
def useful2(a, b, c, d, e, f, g):
...
def bonjour(a, b, c, d, e, f, g):
...
Run Code Online (Sandbox Code Playgroud)
你不需要传递它们.如果你已经import file1在main.py的顶部完成了,你可以直接引用file1.a等.
然而,拥有这么多全局变量闻起来非常糟糕.您可能应该使用类重构.
| 归档时间: |
|
| 查看次数: |
2725 次 |
| 最近记录: |