Abdullah Leghari Abdullah Leghari - 5 months ago 45
Python Question

How do you check in which module a function is defined? Python

How to check to which module a function belongs?


Would return
and so on, if at all.

Answer Source

Functions have a __module__ attribute:

>>> from math import sqrt
>>> sqrt.__module__

You can use the inspect.getmodule() function to get the actual module object for a given object:

>>> from inspect import getmodule
>>> getmodule(sqrt)
<module 'math' from '/Users/mj/Development/Library/buildout.python/python-3.4/lib/python3.4/lib-dynload/math.so'>

inspect.getmodule() works for more than just functions and classes; it'll go through some length to find a module for a given object, based on the metadata on that object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download