lundi 8 février 2016

Getting all functions of python package

I have a multimodule Python package which has one file with class MyClass and few files with functions (one file can contain more than 1 function).

I tried to get all functions in this package.

Firstly, using the following:

modules = []
for importer, module, ispkg in pkgutil.iter_modules(package.__path__):
    modules.extend(module)

but I got only module names, not module objects.

When I tried to get functions of these modules using inspect.getmembers(module, inspect.isfunction), I of course, got an empty collection.

So, how can I got all module objects from the package for further getting functions?

Does an easier way to get all functions from the multifile package exists?

P.S. I need exactly function objects and not only their names.

Edit.

  1. dir(package) gives me only built-in variables:

    ['builtins', 'doc', 'file', 'name', 'package', 'path']

  2. inspect.getmembers(package, inspect.infunction) gives an empty list as well as the same code for each module as I described above.

  3. dir(module) gives me the name list of all methods available for str objects (because, iter_modules() gives only names of modules).

  4. AST... Are you sure, that there isn't simpler solutions?





Aucun commentaire:

Enregistrer un commentaire