Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python view module (variable, function, class)

In Python, if you want to view the variables, functions, and classes in a module, you can use the dir() function or the .__dict__ attribute. The dir() function returns a list of names in the current local scope or the names defined in a module. The .__dict__ attribute returns a dictionary containing the module's attributes.

Here's an example of how to view variables, functions, and classes in a module:

  • Create a module named example_module.py:
# example_module.py
variable_a = "Hello, World!"

def function_a():
    return "This is function_a."

class ClassA:
    def method_a(self):
        return "This is method_a in ClassA."
  • Create a script named main.py to import the example_module and view its contents:
# main.py
import example_module

# Using dir() function
print("Using dir():")
print(dir(example_module))

# Using .__dict__ attribute
print("\nUsing .__dict__:")
for key, value in example_module.__dict__.items():
    if not key.startswith('__'):
        print(f"{key}: {value}")

When you run main.py, you'll see the output like this:

Using dir():
['ClassA', 'function_a', 'variable_a']

Using .__dict__:
variable_a: Hello, World!
function_a: <function function_a at 0x7f4c1d4150d0>
ClassA: <class 'example_module.ClassA'>

In this example, the dir() function returns a list of names defined in the example_module. The .__dict__ attribute returns a dictionary containing the attributes of the example_module. By iterating over the dictionary, we can see the names and values of the variables, functions, and classes defined in the module.

Keep in mind that this approach will also show any imported modules or functions in the module. If you want to view only the attributes that are defined in the module itself, you can filter the results using the inspect module or by checking if the attribute name starts with a specific prefix (e.g., if you follow a naming convention for your functions and classes).

  1. List all variables in a Python module:

    • Description: Use the dir() function to list all variables in a module.
    • Code:
      import my_module
      
      all_variables = dir(my_module)
      print(all_variables)
      
  2. Inspecting functions in a Python module:

    • Description: To inspect functions in a module, use the dir() function and filter for items that are functions.
    • Code:
      import my_module
      
      functions = [name for name in dir(my_module) if callable(getattr(my_module, name))]
      print(functions)
      
  3. Viewing variables in Python script or module:

    • Description: Use the dir() function to view all variables in the current script or module.
    • Code:
      all_variables = dir()
      print(all_variables)
      
  4. Listing classes in a Python module:

    • Description: Use the dir() function and filter for items that are classes to list all classes in a module.
    • Code:
      import my_module
      
      classes = [name for name in dir(my_module) if isinstance(getattr(my_module, name), type)]
      print(classes)
      
  5. Using dir() function to view module contents in Python:

    • Description: The dir() function returns a list of names in the current module or object. It includes variables, functions, classes, and other objects.
    • Code:
      import my_module
      
      module_contents = dir(my_module)
      print(module_contents)
      
  6. Get a list of functions in a Python module:

    • Description: Use the dir() function and filter for items that are functions to get a list of functions in a module.
    • Code:
      import my_module
      
      functions = [name for name in dir(my_module) if callable(getattr(my_module, name))]
      print(functions)
      
  7. Viewing docstrings for functions and classes in Python:

    • Description: Use the __doc__ attribute to access the docstring of a function or class in a module.
    • Code:
      import my_module
      
      docstring_function = my_module.my_function.__doc__
      print(docstring_function)
      
  8. Inspecting imported modules in Python:

    • Description: Use the dir() function and filter for items that are modules to inspect imported modules.
    • Code:
      import my_module
      
      imported_modules = [name for name in dir(my_module) if isinstance(getattr(my_module, name), type(my_module))]
      print(imported_modules)