pyclbr — Python 模块浏览支持

源代码: Lib/pyclbr.py


The pyclbr 模块提供 python 编码模块定义的有关函数、类及方法的有限信息。这些信息足以实现模块浏览器。信息是从 Python 源代码提取的,而不是通过导入模块,因此本模块可以安全地用于不可信代码。此限定使之不可能将本模块用于未在 Python 中实现的模块,包括所有标准和可选扩展模块。

pyclbr. readmodule ( module , path=None )

Return a dictionary mapping module-level class names to class descriptors. If possible, descriptors for imported base classes are included. Parameter module is a string with the name of the module to read; it may be the name of a module within a package. If given, path is a sequence of directory paths prepended to sys.path , which is used to locate the module source code.

This function is the original interface and is only kept for back compatibility. It returns a filtered version of the following.

pyclbr. readmodule_ex ( module , path=None )

Return a dictionary-based tree containing a function or class descriptors for each function and class defined in the module with a def or class statement. The returned dictionary maps module-level function and class names to their descriptors. Nested objects are entered into the children dictionary of their parent. As with readmodule, module names the module to be read and path is prepended to sys.path. If the module being read is a package, the returned dictionary has a key '__path__' whose value is a list containing the package search path.

3.7 版新增: Descriptors for nested definitions. They are accessed through the new children attribute. Each has a new parent attribute.

The descriptors returned by these functions are instances of Function and Class classes. Users are not expected to create instances of these classes.

函数对象

Function instances describe functions defined by def statements. They have the following attributes:

Function. file

Name of the file in which the function is defined.

Function. 模块

描述函数定义的模块名称。

Function. 名称

函数的名称。

Function. lineno

The line number in the file where the definition starts.

Function. parent

For top-level functions, None. For nested functions, the parent.

3.7 版新增。

Function. children

A dictionary mapping names to descriptors for nested functions and classes.

3.7 版新增。

类对象

Class instances describe classes defined by class statements. They have the same attributes as Functions and two more.

Class. file

Name of the file in which the class is defined.

Class. 模块

描述类定义的模块名称。

Class. 名称

The name of the class.

Class. lineno

The line number in the file where the definition starts.

Class. parent

For top-level classes, None. For nested classes, the parent.

3.7 版新增。

Class. children

A dictionary mapping names to descriptors for nested functions and classes.

3.7 版新增。

Class. super

列表化的 Class objects which describe the immediate base classes of the class being described. Classes which are named as superclasses but which are not discoverable by readmodule_ex() are listed as a string with the class name instead of as Class 对象。

Class. 方法

A dictionary mapping method names to line numbers. This can be derived from the newer children dictionary, but remains for back-compatibility.

内容表

上一话题

tabnanny — 歧义缩进的检测

下一话题

py_compile — 编译 Python 源文件

本页