当前位置:网站首页>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
边栏推荐
- Bus message bus
- 2022年全国所有A级景区数据(13604条)
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
- sqlserver多线程查询问题
- This article introduces you to the characteristics, purposes and basic function examples of static routing
- 带你刷(牛客网)C语言百题(第一天)
- 华为机试题素数伴侣
- Brand · consultation standardization
- Config distributed configuration center
- Unity3d learning notes
猜你喜欢
![Stack and queue-p78-8 [2011 unified examination true question]](/img/df/72ba22f1953551943494d661a56a3b.jpg)
Stack and queue-p78-8 [2011 unified examination true question]

关于数据库数据转移的问题,求各位解答下
SVN version management in use replacement release and connection reset

Redhat5 installing vmware tools under virtual machine

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案

Mysql---- import and export & View & Index & execution plan

What books can greatly improve programming ideas and abilities?

Prime partner of Huawei machine test questions

偏执的非合格公司

MySQL view bin log and recover data
随机推荐
2022年全国所有A级景区数据(13604条)
What books can greatly improve programming ideas and abilities?
关于数据库数据转移的问题,求各位解答下
联合索引ABC的几种索引利用情况
【JDBC以及内部类的讲解】
Redhat5 installing vmware tools under virtual machine
How to model and simulate the target robot [mathematical / control significance]
7天零基础能考证HCIA吗?华为认证系统学习路线分享
毕业设计游戏商城
ANR 原理及实践
main函数在import语句中的特殊行为
How can brand e-commerce grow against the trend? See the future here!
Mysql---- import and export & View & Index & execution plan
mysql查看bin log 并恢复数据
健身房如何提高竞争力?
Bus消息总线
MySql用户权限
Initial experience of addresssanitizer Technology
How can flinksql calculate the difference between a field before and after update when docking with CDC?
根据IP获取地市