Python学习笔记(二十四)- 高级的模块话题(Advanced Module Topics)

1.名称以单个下划线开头的模块顶层变量有什么重要意义?
答:当使用 from * 语句形式时,名称以单个下划线开头的模块顶层的变量不会复制到导入作用域内。但是,它们仍然可以通过import 或普通的 from 语句形式访问。 __all__ 列表是类似的,但逻辑相反;它的内容是从 * 中复制出来的唯一名称。


2.当模块的 __name__ 变量是字符串 "__main__" 时,它是什么意思?
答:如果模块的 __name__ 变量是字符串 "__main__" ,则表示该文件作为顶层脚本执行,而不是从程序中的另一个文件导入。也就是说,该文件被用作程序(program),而不是库(library)。这个使用模式变量支持双模式代码(dual-mode code)和测试(tests)。

 

3.如果用户以交互方式键入要测试的模块的名称,您的代码如何导入它?
答:用户输入通常作为字符串进入脚本;要根据其字符串名称导入引用的模块,您可以使用 exec 构建并运行 import 语句,或者在调用 __import__ 或 importlib.import_module 时传递字符串名称。


4.更改 sys.path 与设置 PYTHONPATH 来修改模块搜索路径有何不同?
答:更改 sys.path 只会影响一个正在运行的程序(进程),并且是临时的 - 当程序结束时,更改会消失。 PYTHONPATH 设置存在于操作系统中 - 它们在全局范围内被机器上的所有程序捕获,并且在程序退出后对这些设置的更改会持续。


5.如果模块 __future__ 允许我们从未来(新功能)导入,我们也可以从过去(旧功能?)导入吗?
答:不,我们不能用 Python 从过去导入。我们可以安装(或顽固地使用)旧版本的语言,但最新的Python通常是最好的Python(至少在行内 - 可见2.X长寿!)。

 

一些其它的东西:

1.
(1)You're always in a module in Python. 在Python中,你一直在模块中,甚至是交互模式 如:__main__.py

(2)Minimize module coupling: global variables. 最小化模块耦合:全局变量。

(3)Maximize module cohesion: unified purpose. 最大化模块内聚:统一目的。

(4)Module should rarely change other modules's variables. 模块应该很少改变其他模块的变量。

 

2.

每个模块文件有一个内建属性,被称为 __name__

(1)如果文件作为顶层文件运行,__name__ 被设置为 __main__

(2)如果文件以被导入的方式取而代之,__name__ 被设定为模块的名字

__name__ :(1)作为工具库模块(2)可执行程序

 

3.

import modulename as name #使用name,而不是modulename,等价于:

import modulename

name = modulename

del modulename

 

4.所有查找属性和对象的方法

(1)M.name     按属性限定对象

(2)M.__dict__['name']     手动索引命令空间列表

(3)sys.module['M'].name     手动索引已加载的模块表

(4)getattr(M, 'name')    调用内建的搜索函数

 

5.假设一个情况,你想在程序运行的情况下导入模块,你可以这样做么

>>>import 'string'

SyntaxError: invalid syntax

好像不行啊,那么这样子呢?

>>>X = 'string'
>>>import X

 Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    import X
ModuleNotFoundError: No module named 'X'

嗯?怎么会报 No module named ‘X’ ,仔细一想,被 Python 翻译为想找名为 X.py 的文件,我们当然没有啊。

那么我们应该怎么办呢?

>>>modname = 'string'
>>>exec('import '+ modname) #注意import字符串有个空格
>>>string
<module 'string'...>

好像成功了,那么还有什么办法么?

>>>import importlib
>>>modname = 'time'
>>>string = importlib.import_module(modname)
>>>string
<module 'time' (built-in)>

好像有成功了,有点意思。

 

6. reload 不要和 from 一起使用,和 import 一起搭配比较好。

 

注:转载《Learning Python 5th Edition》[奥莱理]

1. What is significant about variables at the top level of a module whose names begin with a single underscore?
2. What does it mean when a module’s __name__ variable is the string "__main__"?
3. If the user interactively types the name of a module to test, how can your code import it?
4. How is changing sys.path different from setting PYTHONPATH to modify the module search path?
5. If the module __future__ allows us to import from the future, can we also import from the past?

1. Variables at the top level of a module whose names begin with a single underscore are not copied out to the importing scope when the from * statement form is used. They can still be accessed by an import or the normal from statement form, though. The __all__ list is similar, but the logical converse; its contents are the only names that are copied out on a from *.
2. If a module’s __name__ variable is the string "__main__", it means that the file is being executed as a top-level script instead of being imported from another file in the program. That is, the file is being used as a program, not a library. This usage mode variable supports dual-mode code and tests.
3. User input usually comes into a script as a string; to import the referenced module given its string name, you can build and run an import statement with exec, or pass the string name in a call to the __import__ or importlib.import_module.
4. Changing sys.path only affects one running program (process), and is temporary —the change goes away when the program ends. PYTHONPATH settings live in the operating system—they are picked up globally by all your programs on a machine, and changes to these settings endure after programs exit.
5. No, we can’t import from the past in Python. We can install (or stubbornly use) an older version of the language, but the latest Python is generally the best Python (at least within lines—see 2.X longevity!).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值