Search code examples
pythonstringclassdocstring

How to convert a string containing a path and access the class using that string in Python?


I'm looking to take in a string of the format "package.file.module.classname" and use that to access the docstring of the particular class or functions within.

I can import the initial package, but the remaining part of the path remains flexible to any input.

I've attempted using getattr(sys.modules[__name__], classname) but that seems to be limited to within a current package and cannot be extended outside it


Solution

  • After parsing the string into module and class names, I think you need to use importlib to import the module, then use getattr to get the class object from the module by name.

    After that, I was able to get docstrings using inspect

    Here's an example: Say you have a birds module: animals/birds.py:

    class Duck:
        """
        A duck class
        """
        def __init__(self, name):
            self._name = name
    
        def quack(self):
            """
            A quack method
            """
            print(f"{self._name} is quacking")
    

    This code will return a dict with class and method names as keys, and their docstrings as values:

    import importlib
    import inspect
    
    
    def get_docstrings(class_string):
        try:
            # Split the string into module, class name parts
            path_parts = class_string.split('.')
            module_name = '.'.join(path_parts[:-1])
            class_name = path_parts[-1]
            module = importlib.import_module(module_name)
    
            # Retrieve the class object from the module
            class_obj = getattr(module, class_name)
    
            doc_strings = {}
    
            # Return the docstring of the class
            doc_strings[class_name] = class_obj.__doc__
    
            for name, member in inspect.getmembers(class_obj):
                if inspect.isfunction(member):
                    doc_strings[name] = member.__doc__
    
            return doc_strings
    
        except (AttributeError, ImportError, IndexError):
            return None
    
    # Example usage
    docstring = get_docstrings("animals.birds.Duck")
    

    Note that docstrings will have new line escapes and other formatting:

    {
      'Duck': '\n    A duck class\n    ',
      '__init__': None,
      'quack': '\n        A quack method\n        '
    }