当前位置:网站首页>Special behavior of main function in import statement
Special behavior of main function in import statement
2022-07-07 07:04:00 【Sheng Tian Banzi_ Wang Er_ Wang Banxian】
Say first conclusion , import module Will be executed module Function level code , But if it's time to module There is main function , They don't execute .
It sounds complicated , Just look at the code below , There are also some problems that have not been solved , I hope the boss will teach
test_02.py
from test_03 import test_print
x = None
print 'test_02 execute'
def get_x():
global x
print 'get_X'
return x
def set_x():
global x
print 'set_x'
x = 10
if __name__ == '__main__':
print 'execute main'
set_x()
print get_x()
print '===================================='
test_print()
test_03.py
def test_print():
import test_02 as x
print x.x
above test_02
perform , Called test_03
Medium test_print()
, test_print
To print test_02
Global variable in x
, But I found that it was still the default , Why is that ?
Look at the printout
Turned out to be test_03 In the implementation of import At the time of statement , Yes test_02 Function level statements in , And if we change the code again , add id, to glance at :test_02
if __name__ == '__main__':
print 'execute main'
set_x()
print get_x()
print '===================================='
test_print()
print get_x()
print id(x)
test_03
def test_print():
from test_02 import x
print x
print id(x)
Execution results
We found that id Is not the same
in other words test_03 in import x It was not only implemented once test_02 The code in , Not even the same object
But if we put set_x() from main Function to the outside , Look at the results again
id It's the same again , Why is that ? Do you have a boss to explain main Why is function so special
边栏推荐
- 使用TCP/IP四层模型进行网络传输的基本流程
- Data of all class a scenic spots in China in 2022 (13604)
- 数据资产管理与数据安全国内外最新趋势
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
- Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
- Jetpack Compose 远不止是一个UI框架这么简单~
- Advantages of using net core / why
- 品牌·咨询标准化
- MYSQL----导入导出&视图&索引&执行计划
- Lvs+kept (DR mode) learning notes
猜你喜欢
关于数据库数据转移的问题,求各位解答下
DHCP路由器工作原理
MySQL view bin log and recover data
服装门店如何盈利?
华为机试题素数伴侣
This article introduces you to the characteristics, purposes and basic function examples of static routing
网络基础 —— 报头、封装和解包
MOS管参数μCox得到的一种方法
一文带你了解静态路由的特点、目的及配置基本功能示例
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
随机推荐
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
Kotlin之 Databinding 异常
Network foundation - header, encapsulation and unpacking
Config分布式配置中心
【JDBC以及内部类的讲解】
场馆怎么做体育培训?
【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
数据资产管理与数据安全国内外最新趋势
How DHCP router works
Unity3d learning notes
Redhat5 installing vmware tools under virtual machine
How can brand e-commerce grow against the trend? See the future here!
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
Use of completable future
[GNN] graphic gnn:a gender Introduction (including video)
Prime partner of Huawei machine test questions
Jetpack Compose 远不止是一个UI框架这么简单~
Several index utilization of joint index ABC
Please answer the questions about database data transfer
剑指offer-高质量的代码