模块导入和重载

  • 解释:每一个以.py结尾的Python源码文件都是一个模块,其它的文件可以通过导入一个模块读取这个模块的内容。导入从本质上来讲,就是载入另一个文件。
  • 形式:
    # vi script1
    import sys
    print(sys.platform)
    print(2 ** 100)
    x = 'Spam!'
    print(x * 8)
    wq
    
    # ipython3
    In [1]: import script1
    linux
    1267650600228229401496703205376
    Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
    

    默认情况下,导入只会在每次会话的第一次运行,在第一次导入之后,再进行导入操作将不会再工作,甚至在另一个窗口你改变并保存了模块的源码文件。

    In [2]: import script1
    In [3]: import script1
    

    这是有意设计的结果,导入是一个开销很大的操作,以至于每个文件,每个程序运行不能够重复多余一次。但是如果真的想要Python在同一次会话中再 次运行文件(不停止和重新启动会话),需要调用imp标准库模块中可用的reload函数(Python2内置,Python3不是内置了)

    In [4]: from imp import reload
    In [5]: reload(srcript1)
    linux
    1267650600228229401496703205376
    Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
    Out[5]: <module 'script1' from '/usr/local/kaelwork/python/script1.py'> 
    

    这里的from语句直接从一个模块中复制出一个名字,reload函数载入并运行了最新文件的最新代码,如果你已经在另一个窗口中修改并保存了的话。这就 允许了你在当前交互会话的过程中编辑并改进代码。reload函数希望获得的参数是一个已经加载了的模块对象的名称,所以如果在重载之前,请确保已经成功 导入了这个模块,值得注意的是,reload函数在模块对象的名称前面还需要加入括号,import则不需要,reload是一个被调用的函数,而import是一个语句!这也就是为什么你必须把模块名称传递给reload函数u作为括号中的参数,并且这也是在重载时得到了额外一行输出的原因。最后一行输出是reload调用后的返回值的打印显示,reload函数的返回值是一个Python模块对象。

模块的显要特性:属性

  • 解释:宏观角度上讲,模块扮演了一个工具库的角色,一般意义上来说,模块往往就是变量名的封装,被认作是命名空间。在一个包中的变量名就是所谓的属性。也就是说,属性就是绑定在特定的对象上的变量名(就像一个模块)。
  • 形式:一个模块文件的变量名可以通过import和from语句读取,以及reload函数调用
    # vi myfile.py
    title = "The Meaning of Life"
    wq
    
    # ipython3
    In [1]: import myfile
    In [2]: print(myfile.title)
    The Meaning of Life
    

    一般来说,这里的点号表达式代表了object.attribute的语法,可以从任意object中取出任意的属性。作为替代方案,可以通过这样的语句从模块文件中获得(实际上是复制)变量名:

    # ipython3
    In [1]: from myfile import title
    In [2]: print(title)
    The Meaning of Life
    

    import和from很相似,只不过增加了对载入组建的变量名的额外的赋值。从技术上讲,from复制了模块的属性,以便属性能够成为接收者的直接变量。因此,能够直接一title(一个变量)引用导入字符串而不是myfile.title(一个属性)引用。

    # vi treenames.py
    a = 'dead'
    b = 'parrot'
    c = 'sketch'
    print(a,b,c)
    wq
    
    # python3 threenames.py
    dead parrot sketch
    

    所有的这个文件代码运行起来就和第一次从其他地方导入(无论通过import和from)后一样。这个文件的客户端通过import的到了具有属性的模块,而客户端使用from时,则会获得文件变量名的副本。

    # ipython3
    In [1]: import threenames                              //载入整个模块
    dead parrot sketch
    In [2]: threenames.b,threenames.c
    Out[2]: ('parrot', 'sketch')
    In [3]: from threenames import a,b,c                   //复制变量名
    In [4]: b,c
    Out[4]: ('parrot', 'sketch')
    

    一旦模块文件中有多个变量名,内置的dir函数可以获得模块内部的可用的变量名的列表。

    In [5]: dir(threenames)
    Out[5]: 
    ['<strong>builtins</strong>',
     '<strong>cached<strong>',
     '</strong>doc</strong>',
     '<strong>file</strong>',
     '<strong>loader<strong>',
     '</strong>name<strong>',
     '<strong>package</strong>',
     '</strong>spec</strong>',
     'a',
     'b',
     'c']
    

    你会发现,一些以双下划线开头并结尾的变量名是免费获得的,这些通常都是由Python预定义的内置变量名,对于解释器来说有特定的意义。那些通过代码赋值而定义的变量在dir结构的最后显示。

模块和命名空间

  • 模块是Python程序最大的程序结构。通过import语句连接多个模块文件。每个模块文件是一个独立完备的变量包,即一个命名空间。一个模块 文件不能看到其它文件定义的变量名,除非显式地导入了那个文件。每个文件都是一个独立完备的命名空间,即使他们可能拼写相同,一个文件中的变量名是不会与 另一个文件中的变量冲突的。正式由于模块将变量封装为不同的部分,Python具有了能够避免命名冲突的有点。
  • 注意:import和from列出模块名时,都是使用了myfile,没有.py后缀,但是当Python寻找实际文件时,在搜索程序上加入后缀名。系统shell命令中,除了import语句中不同加入后缀名,其它都是加入。
  • exec运行模块文件,exec内置函数调用,是从交互提示模式启动文件而不必导入以及随后的重载的一种方法,每次exec都运行文件的最新版本,而不需要随后重载。
    # ipython3
    In [1]: exec(open('script1.py').read())
    linux
    1267650600228229401496703205376
    Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
    

    但是必须要注意的是,exec的工作机制就好像在调用它的地方粘帖了代码一样,和from一样,对于当前正在使用的变量有潜在的默认覆盖的可能。

    In [2]: x = 999
    In [3]: exec(open('script1.py').read())
    linux
    1267650600228229401496703205376
    Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
    In [4]: x
    Out[4]: 'Spam!'
    

    相反基本的import语句每个进程只运行文件一次,并且它会把文件生成到一个单独的模块名称空间中,以便它的赋值不会改变你的作用域中的变量。为模块名称空间分隔付出的代价是,在修改之后需要重载。

分类: Python3

71 条评论

hedgedoc.uni-ak.ac.at · 2026-09-05 19:38

References:

Mobile pokies no deposit bonus australia hedgedoc.uni-ak.ac.at

mobile pokies payid · 2026-09-17 20:31

References:

$10 payid pokies mobile pokies payid

https://bookmark4you.win · 2026-09-17 23:24

References:

Instant payid pokies https://bookmark4you.win

mobile payid pokies australia · 2026-09-18 00:54

References:

Best payid pokies sites mobile payid pokies australia

VIP payid pokies australia · 2026-09-18 02:14

References:

Trusted payid pokies australia VIP payid pokies australia

http://t.044300.net · 2026-09-18 03:45

References:

Payid pokies free spins http://t.044300.net

https://may22.ru/user/bikeplot6/ · 2026-09-18 11:28

References:

Highest paying payid pokies australia https://may22.ru/user/bikeplot6/

10 dollar deposit pokies payid · 2026-09-19 10:33

References:

Payid pokies app 10 dollar deposit pokies payid

https://wooten-rodriquez-2.hubstack.net · 2026-09-20 08:38

References:

Mobile casino MiFinity Australia https://wooten-rodriquez-2.hubstack.net

winnipegasians.com · 2026-09-20 09:56

References:

EFT casino similar payments winnipegasians.com

$20 deposit MiFinity casino AU · 2026-09-20 17:52

References:

MiFinity casino live dealer Australia $20 deposit MiFinity casino AU

neosurf casino instant withdrawal australia · 2026-09-21 05:03

References:

Safe neosurf casinos australia https://molchanovonews.ru/user/fieldmary6/

https://pad.public.cat · 2026-09-21 12:18

References:

Online casino startguthaben sofort https://pad.public.cat/miag5hN2T8WB5T0BFlyx4g

online casino per sofort · 2026-09-21 19:07

References:

Neues online casino sofortüberweisung https://git.arteneo.pl/arrowpillow7

pad.public.cat · 2026-09-21 23:38

References:

Sofortüberweisung casino einzahlung https://pad.public.cat/Z3Owd3RpSX6DxOBo6VXFXw

codimd.syssec.org · 2026-09-22 00:13

References:

Sofort einzahlung casino https://codimd.syssec.org/xwce5JYCTAaNjhEUReTiOw

https://gaiaathome.eu/ · 2026-09-22 01:28

References:

Online casino sofortüberweisung ab 1 euro https://gaiaathome.eu/gaiaathome/show_user.php?userid=2124003

git.arteneo.pl · 2026-09-22 09:52

References:

Online casino mit sofortüberweisung auszahlung https://git.arteneo.pl/brickpillow7

http://bbs.91tata.com/?16701444 · 2026-09-23 03:02

References:

Einzahlung mit sofort casino http://bbs.91tata.com/?16701444

http://www.sg588.tw/home.php?mod=space&uid=1287591 · 2026-09-23 03:57

References:

Deutsche Casinos mit Sofortüberweisung http://www.sg588.tw/home.php?mod=space&uid=1287591

pandora6666.com · 2026-09-23 04:54

References:

Spielothek sofortüberweisung klarna http://pandora6666.com/?191417

wou.malaysia2host.com · 2026-09-23 05:58

References:

Online Casinos mit Sofortüberweisung http://wou.malaysia2host.com/home.php?mod=space&uid=341996

http://daojianchina.com/ · 2026-09-23 06:02

References:

Online casino schnelle auszahlung sofort http://daojianchina.com/home.php?mod=space&uid=1462821

http://qiaoxiaojun.vip · 2026-09-23 06:22

References:

Online casino mit sofortüberweisung bonus http://qiaoxiaojun.vip/home.php?mod=space&uid=2682447

chuangu.xyz · 2026-09-23 07:12

References:

Direct bank transfer payid pokies https://chuangu.xyz/home.php?mod=space&uid=293988&do=profile&mycenter=1

nlvbang.com · 2026-09-23 08:41

References:

Casino Echtgeld Sofortüberweisung https://www.nlvbang.com/home.php?mod=space&uid=3238591

anonymous payid pokies · 2026-09-23 09:27

References:

10 dollar deposit pokies payid https://md.sigma2.no/ACGYLFgGS5inYo0xMwm-pA

bbs.yp001.net · 2026-09-23 10:12

References:

Casino freispiele mit sofort https://bbs.yp001.net/home.php?mod=space&uid=606274

pandora6666.com · 2026-09-23 10:34

References:

Casino Sofort Einzahlung http://pandora6666.com/?191625

http://gjglm.com/ · 2026-09-23 11:22

References:

Sichere sofort casinos http://gjglm.com/home.php?mod=space&uid=189292

jszst.com.cn · 2026-09-23 11:40

References:

Casino ohne limit sofortüberweisung https://jszst.com.cn/home.php?mod=space&uid=7276342

forum.uookle.com · 2026-09-23 11:48

References:

Online casinos mit sofort https://www.forum.uookle.com/home.php?mod=space&uid=1801970

http://x.kongminghu.com/home.php?mod=space&uid=870956 · 2026-09-23 12:07

References:

Casino 1 euro einzahlung sofortüberweisung http://x.kongminghu.com/home.php?mod=space&uid=870956

88m2.com · 2026-09-23 12:10

References:

Willkommensbonus sofortüberweisung https://www.88m2.com/?301328

oneclickcarehk.com · 2026-09-23 12:12

References:

Auszahlung sofortüberweisung casino http://oneclickcarehk.com/forum/home.php?mod=space&uid=283917

Online Casino Sofort Deutschland · 2026-09-23 12:34

References:

Sofortüberweisung casino ohne registrierung http://bbs.hgzvip.net/home.php?mod=space&uid=307090

fumankong1.cc · 2026-09-23 15:46

References:

Casino per sofortüberweisung http://www.fumankong1.cc/home.php?mod=space&uid=967811

bbs.hgzvip.net · 2026-09-23 16:39

References:

Sofort Casino Zahlungsmethoden http://bbs.hgzvip.net/home.php?mod=space&uid=307111

http://9youro.com/ · 2026-09-23 16:47

References:

Neue Online Casinos mit Sofort http://9youro.com/home.php?mod=space&uid=111102

creativespace.biz · 2026-09-23 17:06

References:

Casino 5 euro einzahlung sofort http://creativespace.biz/discuss/forum/home.php?mod=space&uid=205260

http://www.4001179958.org/ · 2026-09-23 17:56

References:

Casino auszahlung sofort http://www.4001179958.org/?144836

http://x.kongminghu.com/ · 2026-09-23 18:00

References:

Sofort casino mit sofortiger auszahlung http://x.kongminghu.com/home.php?mod=space&uid=871037

guzhen0552.cn · 2026-09-23 18:14

References:

Casino mit sofort auszahlung https://guzhen0552.cn/home.php?mod=space&uid=2527566

bbs.darkml.net · 2026-09-24 01:19

References:

Wie funktioniert Sofortüberweisung im Casino https://bbs.darkml.net/home.php?mod=space&uid=285437

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注