Python class browser support

The pyclbr module in Python's standard library extracts information about functions, classes, and methods defined in a Python module. The information is extracted from the Python source code rather than by importing the module, making it safe for analyzing potentially problematic code.

readmodule() Function

The readmodule() function returns a dictionary mapping module-level class names to class descriptors. It takes a module name as parameter and may include modules within packages ?

import pyclbr

mod = pyclbr.readmodule("socket")

def show(c):
    s = "class " + c.name
    print(s + ":")
    methods = c.methods.items()
    for method, lineno in methods:
        print("  def " + method)
    print()

for k, v in mod.items():
    show(v)
class IntEnum:

class IntFlag:
  def _missing_
  def _create_pseudo_member_
  def __or__
  def __and__
  def __xor__
  def __invert__

class _GiveupOnSendfile:

class socket:
  def __init__
  def __enter__
  def __exit__
  def __repr__
  def __getstate__
  def dup
  def accept
  def makefile
  def _sendfile_use_sendfile
  def _sendfile_use_send
  def _check_sendfile_params
  def sendfile
  def _decref_socketios
  def _real_close
  def close
  def detach
  def family
  def type
  def get_inheritable
  def set_inheritable

class SocketIO:
  def __init__
  def readinto
  def write
  def readable
  def writable
  def seekable
  def fileno
  def name
  def mode
  def close

readmodule_ex() Function

The readmodule_ex() function returns a dictionary containing descriptors for both functions and classes defined in the module. This provides more comprehensive information than readmodule() ?

import pyclbr

x = pyclbr.readmodule_ex('socket')

for k, v in x.items():
    print(k, v)
IntEnum <pyclbr.Class object at 0x000002095D7D0048>
IntFlag <pyclbr.Class object at 0x000002095D7D04E0>
_intenum_converter <pyclbr.Function object at 0x000002095D82F940>
_GiveupOnSendfile <pyclbr.Class object at 0x000002095D822898>
socket <pyclbr.Class object at 0x000002095D8227B8>
fromfd <pyclbr.Function object at 0x000002095D8340B8>
fromshare <pyclbr.Function object at 0x000002095D82FEF0>
socketpair <pyclbr.Function object at 0x000002095D834128>
SocketIO <pyclbr.Class object at 0x000002095D82FA20>
getfqdn <pyclbr.Function object at 0x000002095D8344E0>
create_connection <pyclbr.Function object at 0x000002095D834518>
getaddrinfo <pyclbr.Function object at 0x000002095D834550>

Analyzing Custom Modules

These functions work with custom modules as well. Let's create a sample module called triangles.py ?

# triangles.py
import math

class Triangle:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
    
    def area(self):
        s = (self.a + self.b + self.c) / 2
        area = math.sqrt(s * (s - self.a) * (s - self.b) * (s - self.c))
        return area

class EquiTriangle(Triangle):
    def __init__(self, a):
        b = a
        c = a
        super().__init__(a, b, c)
    
    def area(self):
        area = math.sqrt(3) * pow(self.a, 2) / 4
        return area

Now we can analyze the structure of our custom module ?

import pyclbr

br = pyclbr.readmodule_ex('triangles')
for name, descriptor in br.items():
    print(name, descriptor.methods)
Triangle {'__init__': 3, 'area': 7}
EquiTriangle {'__init__': 12, 'area': 16}

Object Attributes

The pyclbr module defines two main object types: Function and Class objects, each with specific attributes.

Function Object Attributes

Attribute Description
file Name of the file in which the function is defined
module The name of the module defining the function
name The name of the function
lineno The line number where the definition starts
parent For top-level functions: None. For nested functions: the parent
children Dictionary mapping names to descriptors for nested functions and classes

Class Object Attributes

Class objects have all Function attributes plus two additional ones ?

Attribute Description
super List of Class objects describing immediate base classes
methods Dictionary mapping method names to line numbers

Conclusion

The pyclbr module provides a safe way to analyze Python module structure without importing. Use readmodule() for classes only or readmodule_ex() for both classes and functions.

Updated on: 2026-03-25T05:11:49+05:30

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements