当前位置:网站首页>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
边栏推荐
- ESXI挂载移动(机械)硬盘详细教程
- Bus message bus
- SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
- Initial experience of addresssanitizer Technology
- Jetpack compose is much more than a UI framework~
- What books can greatly improve programming ideas and abilities?
- 分布式id解决方案
- The latest trends of data asset management and data security at home and abroad
- impdp的transform参数的测试
- Kotlin之 Databinding 异常
猜你喜欢
Use of completable future
What books can greatly improve programming ideas and abilities?
毕业设计游戏商城
JESD204B时钟网络
大咖云集|NextArch基金会云开发Meetup来啦
Stack and queue-p79-9
大促过后,销量与流量兼具,是否真的高枕无忧?
mysql查看bin log 并恢复数据
This article introduces you to the characteristics, purposes and basic function examples of static routing
Matlab tips (29) polynomial fitting plotfit
随机推荐
ViewModelProvider.of 过时方法解决
CompletableFuture使用详解
String (explanation)
MOS管参数μCox得到的一种方法
Unable to debug screen program with serial port
华为机试题素数伴侣
MySql用户权限
什么情况下考虑分库分表
Cloudcompare point pair selection
How Oracle backs up indexes
linux系统rpm方式安装的mysql启动失败
分布式id解决方案
Sqlserver multithreaded query problem
The startup of MySQL installed in RPM mode of Linux system failed
The latest trends of data asset management and data security at home and abroad
Brand · consultation standardization
Master-slave replication principle of MySQL
MATLAB小技巧(30)非线性拟合 lsqcurefit
Prompt for channel security on the super-v / device defender side when installing vmmare
Initial experience of addresssanitizer Technology