w3resource

Python: Get the actual module object for a given object

Python Basic: Exercise-126 with Solution

Get Module Object

Write a Python program to get the actual module object for a given object.

Sample Solution-1:

Python Code:

# Import the 'getmodule' function from the 'inspect' module.
from inspect import getmodule

# Import the 'sqrt' function from the 'math' module.
from math import sqrt

# Use the 'getmodule' function to get the module where the 'sqrt' function is defined.
# Then, print the module information for the 'sqrt' function.
print(getmodule(sqrt))

Sample Output:

<module 'math' (built-in)>

Flowchart:

Flowchart: Get the actual module object for a given object.

Sample Solution-2:

Python Code:

# Import the 'inspect' module.
import inspect

# Define a function named 'add' that takes two arguments, 'x' and 'y'.
def add(x, y):
    return x + y

# Use the 'inspect.getmodule' function to get the module where the 'add' function is defined.
# Then, print the module information for the 'add' function.
print(inspect.getmodule(add))

Sample Output:

<module '__main__' from '/tmp/sessions/5ced515afe46d808/main.py'>

Flowchart:

Flowchart: Get the actual module object for a given object.

Python Code Editor:

 

Previous: Write a Python program to sum of all counts in a collections.
Next: Write a Python program to check if an integer fits in 64 bits.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/python-basic-exercise-126.php