a module who knows its caller
exarkun at intarweb.us
Fri Apr 11 18:31:27 CEST 2003
On Fri, Apr 11, 2003 at 09:12:35AM -0700, Michele Simionato wrote:
> 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.
The inspect module can certainly do this with "magically", but I would
recommend against this kind of thing for anything other than getting extra
information for debugging (and even then, I would recommend pdb first).
Examinations are formidable even to the best prepared, for
even the greatest fool may ask more the the wisest man can answer.
-- C.C. Colton
up 22 days, 13:01, 5 users, load average: 0.99, 0.98, 0.99
More information about the Python-list