pyclbr --- Python 模組瀏覽器支援

原始碼:Lib/pyclbr.py


Thepyclbr module provides limited information about thefunctions, classes, and methods defined in a Python-coded module. Theinformation is sufficient to implement a module browser. Theinformation is extracted from the Python source code rather than byimporting the module, so this module is safe to use with untrusted code.This restriction makes it impossible to use this module with modules notimplemented in Python, including all standard and optional extensionmodules.

pyclbr.readmodule(module,path=None)

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

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

pyclbr.readmodule_ex(module,path=None)

Return a dictionary-based tree containing a function or classdescriptors for each function and class defined in the module with adef orclass statement. The returned dictionary mapsmodule-level function and class names to their descriptors. Nestedobjects are entered into the children dictionary of their parent. Aswith readmodule,module names the module to be read andpath isprepended to sys.path. If the module being read is a package, thereturned dictionary has a key'__path__' whose value is a listcontaining the package search path.

在 3.7 版被加入:Descriptors for nested definitions. They are accessed through thenew children attribute. Each has a new parent attribute.

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

函式物件

classpyclbr.Function

ClassFunction instances describe functions defined by defstatements. They have the following attributes:

file

Name of the file in which the function is defined.

module

The name of the module defining the function described.

name

The name of the function.

lineno

The line number in the file where the definition starts.

parent

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

在 3.7 版被加入.

children

Adictionary mapping names to descriptors for nested functions andclasses.

在 3.7 版被加入.

is_async

True for functions that are defined with theasync prefix,False otherwise.

在 3.10 版被加入.

Class Objects

classpyclbr.Class

ClassClass instances describe classes defined by classstatements. They have the same attributes asFunctionsand two more.

file

Name of the file in which the class is defined.

module

The name of the module defining the class described.

name

The name of the class.

lineno

The line number in the file where the definition starts.

parent

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

在 3.7 版被加入.

children

A dictionary mapping names to descriptors for nested functions andclasses.

在 3.7 版被加入.

super

A list ofClass objects which describe the immediate baseclasses of the class being described. Classes which are named assuperclasses but which are not discoverable byreadmodule_ex()are listed as a string with the class name instead of asClass objects.

methods

Adictionary mapping method names to line numbers.This can be derived from the newerchildren dictionary,but remains forback-compatibility.