当前位置:网站首页>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
边栏推荐
猜你喜欢
How to share the same storage among multiple kubernetes clusters
剑指offer-高质量的代码
Jetpack Compose 远不止是一个UI框架这么简单~
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Stack and queue-p78-8 [2011 unified examination true question]
Can 7-day zero foundation prove HCIA? Huawei certification system learning path sharing
How can brand e-commerce grow against the trend? See the future here!
Several index utilization of joint index ABC
How to model and simulate the target robot [mathematical / control significance]
LVS+Keepalived(DR模式)学习笔记
随机推荐
Redhat5 installing vmware tools under virtual machine
FPGA课程:JESD204B的应用场景(干货分享)
JESD204B时钟网络
Maze games based on JS
After the promotion, sales volume and flow are both. Is it really easy to relax?
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
Sword finger offer high quality code
String (explanation)
Complete process of MySQL SQL
Initial experience of addresssanitizer Technology
一文带你了解静态路由的特点、目的及配置基本功能示例
使用net core优势/为什么使用
Networkx绘图和常用库函数坐标绘图
Leetcode t1165: log analysis
【mysqld】Can't create/write to file
数据资产管理与数据安全国内外最新趋势
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
Stack and queue-p79-10 [2014 unified examination real question]
JWT的基础介绍
联合索引ABC的几种索引利用情况