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)
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:
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."
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).
List all variables in a Python module:
dir()
function to list all variables in a module.import my_module all_variables = dir(my_module) print(all_variables)
Inspecting functions in a Python module:
dir()
function and filter for items that are functions.import my_module functions = [name for name in dir(my_module) if callable(getattr(my_module, name))] print(functions)
Viewing variables in Python script or module:
dir()
function to view all variables in the current script or module.all_variables = dir() print(all_variables)
Listing classes in a Python module:
dir()
function and filter for items that are classes to list all classes in a module.import my_module classes = [name for name in dir(my_module) if isinstance(getattr(my_module, name), type)] print(classes)
Using dir()
function to view module contents in Python:
dir()
function returns a list of names in the current module or object. It includes variables, functions, classes, and other objects.import my_module module_contents = dir(my_module) print(module_contents)
Get a list of functions in a Python module:
dir()
function and filter for items that are functions to get a list of functions in a module.import my_module functions = [name for name in dir(my_module) if callable(getattr(my_module, name))] print(functions)
Viewing docstrings for functions and classes in Python:
__doc__
attribute to access the docstring of a function or class in a module.import my_module docstring_function = my_module.my_function.__doc__ print(docstring_function)
Inspecting imported modules in Python:
dir()
function and filter for items that are modules to inspect imported modules.import my_module imported_modules = [name for name in dir(my_module) if isinstance(getattr(my_module, name), type(my_module))] print(imported_modules)