Posts mit dem Label introspection werden angezeigt. Alle Posts anzeigen
Posts mit dem Label introspection werden angezeigt. Alle Posts anzeigen

Donnerstag, 31. März 2011

Python 002: Version Introspection

When working with Python 2.x and Python 3.x at the same time, its quite probably that the version of the currently running version of Python becomes a question of interest.
One can obtain the version of the running Python instance by issueing
from sys import version_info
if version_info < (3,0):
print version_info
else:
print(version_info)

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.