Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1762149
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-08-12 20:19:36

6.4. Using sys.modules

Sys.moudles.的使用

Modules, like everything else in Python, are objects. Once imported, you can always get a reference to a module through the global dictionary sys.modules.

模块,正如Python中的所有的对象一样,都是对象。一旦导入,你就可以通过全局字典sys.modules的调用来获得第一个模块的引用。

Example 6.12. Introducing sys.modules

6.12 sys.modules.介绍

  1. >>> import sys
  2. >>> print '\n'.join(sys.modules.keys())
  3. win32api
  4. os.path
  5. os
  6. exceptions
  7. __main__
  8. ntpath
  9. nt
  10. sys
  11. __builtin__
  12. site
  13. signal
  14. UserDict
  15. stat

The sys module contains system-level information, such as the version of Python you're running (sys.version or sys.version_info), and system-level options such as the maximum allowed recursion depth (sys.getrecursionlimit() and sys.setrecursionlimit()).

Sys 模块包含了系统级的信息,比如Python的版本,你真在运行的系统(sys.version Or sys.version_info)以及系统级的选项比如最大的允许迭代深度(sys.getrecursionlimit() sys.setreecursionlimit().

sys.modules is a dictionary containing all the modules that have ever been imported since Python was started; the key is the module name, the value is the module object. Note that this is more than just the modules your program has imported. Python preloads some modules on startup, and if you're using a Python IDE,sys.modules contains all the modules imported by all the programs you've run within the IDE.

Sys 模块 是一个字典,它包含了自从Python启动后,你所导入的所有模块:键为模块的名字,值就是模块对象。请注意:实际的模块要远远比你所导入的模块多。Python在启动的时候,预先导入一些模块。如果你正在使用PythonIDEsys.module包含了所有你使用IDe正在运行的程序所导入的模块。

This example demonstrates how to use sys.modules.

本例展示了如何使用sys.modules.

Example 6.13. Using sys.modules

6.13 使用sys.modules.

  1. >>> import fileinfo
  2. >>> print '\n'.join(sys.modules.keys())
  3. win32api
  4. os.path
  5. os
  6. fileinfo
  7. exceptions
  8. __main__
  9. ntpath
  10. nt
  11. sys
  12. __builtin__
  13. site
  14. signal
  15. UserDict
  16. stat
  17. >>> fileinfo
  18. <module 'fileinfo' from 'fileinfo.pyc'>
  19. >>> sys.modules["fileinfo"]
  20. <module 'fileinfo' from 'fileinfo.pyc'>

As new modules are imported, they are added to sys.modules. This explains why importing the same module twice is very fast: Python has already loaded and cached the module in sys.modules, so importing the second time is simply a dictionary lookup.

当新的模块被导入的时候,它们被加入到sys.modules这就解释了为什么导入相同的模块两次,效率为什么这么高的原因:Python早已经在sys.modules 加载并缓存了该模块,因此第二次导入模块就是简单的查找。

Given the name (as a string) of any previously-imported module, you can get a reference to the module itself through the sys.modules dictionary.

假定给出任意先前导入模块的名字(以字符串的形式),你就能通过sys.modules字典来获得对该模块的一个引用。

The next example shows how to use the __module__ class attribute with the sys.modules dictionary to get a reference to the module in which a class is defined.

接下里的这个例子展示了如何在使用sys,modules 字典调,用 __module__类属性来获得,类所在模块的一个引用。

Example 6.14. The __module__ Class Attribute

6.14 __module__ 类属性

  1. >>> from fileinfo import MP3FileInfo
  2. >>> MP3FileInfo.__module__
  3. 'fileinfo'
  4. >>> sys.modules[MP3FileInfo.__module__]
  5. <module 'fileinfo' from 'fileinfo.pyc'>

Every Python class has a built-in class attribute __module__, which is the name of the module in which the class is defined.

每一个Python类都有一个内置的类属性__module__,该属性就是类所在模块的名字

Combining this with the sys.modules dictionary, you can get a reference to the module in which a class is defined.

将该属性同sys.modules字典结合,你同样可以获得定义了类的模块的名字

Now you're ready to see how sys.modules is used in fileinfo.py, the sample program introduced in Chapter 5. This example shows that portion of the code.

现在你在准备看sys.modules模块如何在fileinfo.py文件中使用,该程序在第五章引入。本例显示了改代码的一部分。

Example 6.15. sys.modules in fileinfo.py

6.15 fileinfo文件中的sys.moudles

  

  1. def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):
  2.         "get file info class from filename extension"
  3.         subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]
  4.         return hasattr(module, subclass) and getattr(module, subclass) or FileInfo

This is a function with two arguments; filename is required, but module is optional and defaults to the module that contains the FileInfo class. This looks inefficient, because you might expect Python to evaluate the sys.modules expression every time the function is called. In fact, Python evaluates default expressions only once, the first time the module is imported. As you'll see later, you never call this function with a module argument, so module serves as a function-level constant.

该函数含有两个参数,文件名字是必须的,但是模块名字是可选的,其默认值是包含了类FileInof类的模块。这看起来效率很低,因为在每次被调用的时候,你都想让Python来求sys.moudles表达式的值。实际上,Python对默认表达式仅求值一次,也就是该模块被导入的时候。正如稍后你看到的那样,你调用函数时从不带模块参数,因此模块名字看起来像一个函数级的常量。

You'll plow through this line later, after you dive into the os module. For now, take it on faith that subclass ends up as the name of a class, like MP3FileInfo.

稍后你自己在研究这行,当你深入掌握了os模块。现在,牢牢记住子类是以类的名字结束,如MP3Fileinfo

You already know about getattr, which gets a reference to an object by name. hasattr is a complementary function that checks whether an object has a particular attribute; in this case, whether a module has a particular class (although it works for any object and any attribute, just like getattr). In English, this line of code says, “If this module has the class named by subclass then return it, otherwise return the base class FileInfo.

你已经掌握了getattr方法,它通过名字来获取对象的引用”hasattr是一个补充函数,它主要是用来检测一个对象是否具有某个特殊的属性。在本例中,用来此时一个模块是否有一个特定的类(尽管它适用于任何对象和任何属性,比如getattr)。用英语翻译过来说来说,本行代码的意思就是如果该模块含有一个用子类来命名的类,那么返回该类,如果没有返回基类FileInfo.

 

阅读(800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~