As already stated in the comments you can avoid this (since it is bad design and complicates things a lot) adding a parameter to that function. Or you could write two versions of this function if the code inside is much different from time to time.
Anyway, if you want to know from where your function got called you need the inspect module. I'm not an expert at it, but I don't think it's too hard to obtain the stack frame that called the function and from there understand which script called it.
Update:
If you really want to use inspect
and do the ugly thing, here's a minimal working example:
#file a.py
import inspect
def my_func():
dad_name = inspect.stack()[1][1]
if inspect.getmodulename(dad_name) == 'b': #or whatever check on the filename
print 'You are module b!'
elif inspect.getmodulename(dad_name) == 'c':
print 'You are module c!'
else:
print 'You are not b nor c!'
#file b.py
import a
a.my_func()
#file c.py
import a
a.my_func()
#file d.py
import a
a.my_func()
Output:
$ python b.py
You are module b!
$ python c.py
You are module c!
$ python d.py
You are not b nor c!
If you want to add a parameter to the function:
#file a.py
def my_func(whichmod=None):
if whichmod == 'b':
print 'You are module b!'
elif whichmod == 'c':
print 'You are module c!'
else:
print 'You are not B nor C!'
#files b.py/c.py
import a
a.my_func(whichmod='b') # or 'c' in module c
#file d.py
import a
a.my_func()
The output is the same.