I think you're going to need to roll your own version, but the standard-library inspect
module will get you most of what you need here.
inspect.ismethod()
and inspect.isfunction()
will help determine if the function is bound (defined in a class) or not, and thus whether you're looking for a class or a module (you hope... cause a function might be defined in another scope than a module... e.g. as a locally defined function within scope of another function or method, or as a lambda (same thing really))
Note in particular that the im_class
of a method won't retrieve you the class the method was defined in. But you can (using inspect.getmro()
) follow the class hierarchy and find the 'most recent' class that the method was defined in.
BTW - I'm wondering whether modifying a module will be what you want... in many cases the names within that module may already be imported elsewhere...) In particular I'd be worried (for you and your future sanity) if you have ruby metaprogramming experience and are trying to replicate that in python - it's very different and best not to try to do rubyish things in python.