a module who knows its caller

Michele Simionato mis6 at pitt.edu
Fri Apr 11 18:12:35 CEST 2003


I have a module (say m.py) that can be called by different scripts (say a.py 
and b.py). I want m.py to be able to tell if it has been called by a.py or
by b.py.

What I do now is to pass the global name __file__ to a function m.beencalledby:
something like

# a.py
import m
m.beencalledby(__file__)  # __file__ is a.py


# b.py
import m
m.beencalledby(__file__)  # __file__ is b.py

# m.py

def beencalledby(fname): print 'I have been called by',fname

it works, but I wonder if there is a more elegant solution.

TIA,

                                   Michele




More information about the Python-list mailing list