python全局变量的使用

整体目标

跨文件使用全局变量,控制一下参数的设置。

问题情景

在某一文件夹下存在三个文件,其中文件test2.pytest1.pytest2.py同时引用。但是因为引用方式不同,导致在整个程序的空间中,出现两个相同路径但名称的不同的模块。此时,在test1test3中导入的test2不是同一个test2

# 文件说明
- test1.py
- test3.py
- test_dic
	- test2.py
# path: test_dic/test2.py
x = 10

def print_x():
    print(x)
# path: test1
import test_dic.test2
import test3
import sys

print(test_dic.test2.x)

test_dic.test2.x = 20

test_dic.test2.print_x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值