a module who knows its caller

Batista, Facundo FBatista at uniFON.com.ar
Fri Apr 11 18:53:16 CEST 2003


I already made this routine

def getfather():	
	stack = inspect.stack()
	grandfather = stack[2][3]
	return grandfather

So, you use it in your function like this

	...
	myfather = getfather()
	...

Remember that the father of your function is the grandfather of getfather().

Any question, just ask!

Facundo


#- -----Mensaje original-----
#- De: mis6 at pitt.edu [mailto:mis6 at pitt.edu]
#- Enviado el: Viernes 11 de Abril de 2003 1:13 PM
#- Para: python-list at python.org
#- Asunto: a module who knows its caller
#- 
#- 
#- 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
#- -- 
#- http://mail.python.org/mailman/listinfo/python-list
#- 





More information about the Python-list mailing list