• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`pyclbr` --- Python class browser support
2==============================================
3
4.. module:: pyclbr
5   :synopsis: Supports information extraction for a Python class browser.
6
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9**Source code:** :source:`Lib/pyclbr.py`
10
11--------------
12
13The :mod:`pyclbr` module can be used to determine some limited information
14about the classes, methods and top-level functions defined in a module.  The
15information provided is sufficient to implement a traditional three-pane
16class browser.  The information is extracted from the source code rather
17than by importing the module, so this module is safe to use with untrusted
18code.  This restriction makes it impossible to use this module with modules
19not implemented in Python, including all standard and optional extension
20modules.
21
22
23.. function:: readmodule(module, path=None)
24
25   Read a module and return a dictionary mapping class names to class
26   descriptor objects.  The parameter *module* should be the name of a
27   module as a string; it may be the name of a module within a package.  The
28   *path* parameter should be a sequence, and is used to augment the value
29   of ``sys.path``, which is used to locate module source code.
30
31
32.. function:: readmodule_ex(module, path=None)
33
34   Like :func:`readmodule`, but the returned dictionary, in addition to
35   mapping class names to class descriptor objects, also maps top-level
36   function names to function descriptor objects.  Moreover, if the module
37   being read is a package, the key ``'__path__'`` in the returned
38   dictionary has as its value a list which contains the package search
39   path.
40
41
42.. _pyclbr-class-objects:
43
44Class Objects
45-------------
46
47The :class:`Class` objects used as values in the dictionary returned by
48:func:`readmodule` and :func:`readmodule_ex` provide the following data
49attributes:
50
51
52.. attribute:: Class.module
53
54   The name of the module defining the class described by the class descriptor.
55
56
57.. attribute:: Class.name
58
59   The name of the class.
60
61
62.. attribute:: Class.super
63
64   A list of :class:`Class` objects which describe the immediate base
65   classes of the class being described.  Classes which are named as
66   superclasses but which are not discoverable by :func:`readmodule` are
67   listed as a string with the class name instead of as :class:`Class`
68   objects.
69
70
71.. attribute:: Class.methods
72
73   A dictionary mapping method names to line numbers.
74
75
76.. attribute:: Class.file
77
78   Name of the file containing the ``class`` statement defining the class.
79
80
81.. attribute:: Class.lineno
82
83   The line number of the ``class`` statement within the file named by
84   :attr:`~Class.file`.
85
86
87.. _pyclbr-function-objects:
88
89Function Objects
90----------------
91
92The :class:`Function` objects used as values in the dictionary returned by
93:func:`readmodule_ex` provide the following attributes:
94
95
96.. attribute:: Function.module
97
98   The name of the module defining the function described by the function
99   descriptor.
100
101
102.. attribute:: Function.name
103
104   The name of the function.
105
106
107.. attribute:: Function.file
108
109   Name of the file containing the ``def`` statement defining the function.
110
111
112.. attribute:: Function.lineno
113
114   The line number of the ``def`` statement within the file named by
115   :attr:`~Function.file`.
116
117