当前位置:网站首页>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
边栏推荐
- Config分布式配置中心
- SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
- 毕业设计游戏商城
- How can flinksql calculate the difference between a field before and after update when docking with CDC?
- 大咖云集|NextArch基金会云开发Meetup来啦
- How DHCP router works
- 【mysqld】Can't create/write to file
- Leetcode T1165: 日志分析
- DHCP路由器工作原理
- 请教一个问题,flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取全量数据
猜你喜欢
What books can greatly improve programming ideas and abilities?
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
Anr principle and Practice
健身房如何提高竞争力?
AVL树的实现
Basic process of network transmission using tcp/ip four layer model
剑指offer-高质量的代码
After the promotion, sales volume and flow are both. Is it really easy to relax?
Leetcode t1165: log analysis
mysql查看bin log 并恢复数据
随机推荐
Get the city according to IP
Multidisciplinary integration
Libcurl returns curlcode description
Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
How to install swoole under window
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
Lvs+kept (DR mode) learning notes
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
MySQL view bin log and recover data
华为机试题素数伴侣
数据资产管理与数据安全国内外最新趋势
Algorithm --- bit count (kotlin)
[GNN] graphic gnn:a gender Introduction (including video)
How Oracle backs up indexes
Tool class: object to map hump to underline underline hump
请问 flinksql对接cdc时 如何实现计算某个字段update前后的差异 ?
【mysqld】Can't create/write to file
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
ANR 原理及实践
impdp的transform参数的测试