当前位置:网站首页>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
边栏推荐
- Data of all class a scenic spots in China in 2022 (13604)
- What books can greatly improve programming ideas and abilities?
- 大促过后,销量与流量兼具,是否真的高枕无忧?
- Unable to debug screen program with serial port
- MATLAB小技巧(29)多项式拟合 plotfit
- 企業如何進行數據治理?分享數據治理4個方面的經驗總結
- Use of completable future
- This article introduces you to the characteristics, purposes and basic function examples of static routing
- 请教一下,监听pgsql ,怎样可以监听多个schema和table
- Network foundation - header, encapsulation and unpacking
猜你喜欢

Complete process of MySQL SQL

Bus消息总线

偏执的非合格公司

MYSQL----导入导出&视图&索引&执行计划

Prime partner of Huawei machine test questions
![How to model and simulate the target robot [mathematical / control significance]](/img/bd/79f6338751b6773859435c54430ec3.png)
How to model and simulate the target robot [mathematical / control significance]

MySQL的主从复制原理
![[GNN] graphic gnn:a gender Introduction (including video)](/img/b3/0855885dafa7afaa7375b8d2195974.png)
[GNN] graphic gnn:a gender Introduction (including video)

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

ANR 原理及实践
随机推荐
MySQL SQL的完整处理流程
[noi simulation] regional division (conclusion, structure)
Distributed ID solution
unity3d学习笔记
Bus message bus
Get the city according to IP
CompletableFuture使用详解
Anr principle and Practice
MOS管参数μCox得到的一种方法
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
网络基础 —— 报头、封装和解包
How to do sports training in venues?
使用net core优势/为什么使用
Networkx绘图和常用库函数坐标绘图
使用TCP/IP四层模型进行网络传输的基本流程
Use of completable future
Take you to brush (niuke.com) C language hundred questions (the first day)
How can gyms improve their competitiveness?