Donnerstag, 31. März 2011

Python 001: Module introspection

Python has great self introspection capabilities.
Sometimes flow of control runs through different modules. If I want to know which module is currently in the scope, here is how I can do this:


#!/usr/bin/env python3

import os.path
def getModuleName():
print("Current module:".ljust(30), os.path.abspath(__file__))

getModuleName()


Adding the above to the module should make it possible to get the location and name of the module in scope at runtime.

Keine Kommentare:

Kommentar veröffentlichen