Python Modules with the dir() Function
Let's get to know the dir() function and how it helps us explore Python modules. This function provides a sorted list of all names available in a module when the module has been imported entirely using the import instruction (not from module import...). By using dir(module), you can discover the names of functions, classes, and other entities within the module. Here's an example using the math module:
import math
for name in dir(math):
print(name, end="\t")
Running this code will show the names of entities within the math module, including familiar ones like sin(), cos(), and sqrt(). You might also notice names starting with "__" - we'll learn more about them when we explore writing our own modules. While using dir() directly in code might not be very common, it's handy when you want to check a module's contents before writing your code. Additionally, you can execute the dir() function directly in the Python console IDLE to explore modules without creating a separate script.