python 命名空间 重载模块
2022-04-05 16:59 jym蒟蒻 阅读(71) 评论(0) 编辑 收藏 举报文章目录
- 模块命名空间:
- 重载模块:
#module2.py
print('starting to load...')
import sys
name=42
def func():pass
class klass:pass
print('done loading.')
>>> import module2
starting to load...
done loading.
>>> module2.sys
<module 'sys' (built-in)>
>>> module2.name
42
>>> module2.func
<function func at 0x000001FB71072E18>
>>> module2.klass
<class 'module2.klass'>
>>> dir(module2)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'func', 'klass', 'name', 'sys']
#moda.py:
X=88
def f():
global X
X=99
#modb.py:
X=11
import moda
moda.f()
print(X,moda.X)
#run modb.py:
>>>
========================= RESTART: D:/python/modb.py =========================
11 99
#mod3.py:
X=3
#mod2.py:
X=2
import mod3
print(X,end=' ')
print(mod3.X)
#mod1.py:
X=1
import mod2
print(X,end=' ')
print(mod2.X,end=' ')
print(mod2.mod3.X)
#run mod1.py:
>>>
========================= RESTART: D:/python/mod1.py =========================
2 3
1 2 3
#changer.py:
message='First version'
def printer():
print(message)
>>> import changer
>>> changer.printer()
First version
#改变changer.py并保存:
message = 'After editing'