当前位置:网站首页>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
边栏推荐
- 2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
- 2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
- 多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
- mysql查看bin log 并恢复数据
- 网络基础 —— 报头、封装和解包
- 大促过后,销量与流量兼具,是否真的高枕无忧?
- Networkx drawing and common library function coordinate drawing
- DHCP路由器工作原理
- Master-slave replication principle of MySQL
- 品牌·咨询标准化
猜你喜欢

LVS+Keepalived(DR模式)学习笔记

化工园区危化品企业安全风险智能化管控平台建设四大目标
![Stack and queue-p79-10 [2014 unified examination real question]](/img/7f/e5bba2f64bb77e1781076361c32f01.jpg)
Stack and queue-p79-10 [2014 unified examination real question]

Jmeter 5.5版本发布说明

Graduation design game mall

RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决

基于JS的迷宫小游戏

华为机试题素数伴侣

Matlab tips (30) nonlinear fitting lsqcurefit

JDBC database connection pool usage problem
随机推荐
MySQL user permissions
This article introduces you to the characteristics, purposes and basic function examples of static routing
String (explanation)
Data of all class a scenic spots in China in 2022 (13604)
Graduation design game mall
2022/07/04学习记录
Several index utilization of joint index ABC
服装门店如何盈利?
常用函数detect_image/predict
途家、木鸟、美团……民宿暑期战事将起
毕业设计游戏商城
请教一个问题,flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取全量数据
ViewModelProvider.of 过时方法解决
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
MySQL SQL的完整处理流程
Bus message bus
MySQL (x)
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]