472,096 Members | 1,109 Online

# Function Names on a function call

Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

I need a way to figure out which function made a function call to common_func.

I don't know if I am making sense.

Thanks,
-V
Jul 17 '07 #1
8 1640
ilikepython
844 Expert 512MB
Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

I need a way to figure out which function made a function call to common_func.

I don't know if I am making sense.

Thanks,
-V
You could have an argument in common_func that gets passed different values in func1 and func2:
Expand|Select|Wrap|Line Numbers
1. def common_func(call_func):
2.     if call_func == 1:
3.         # called from func1
4.     if call_func == 2:
5.         # called from func2
6.
7. def func1():
8.     common_func(1)
9.
10. def func2():
11.     common_func(2)
12.
Why do you need that?
Jul 17 '07 #2
bartonc
6,596 Expert 4TB
Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

I need a way to figure out which function made a function call to common_func.

I don't know if I am making sense.

Thanks,
-V
If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:
Expand|Select|Wrap|Line Numbers
1. import inspect
2.
3. class FunctionOwner:
4.     def __init__(self):
5.         self.caller1 = self.func1.im_func.func_code
6.         self.caller2 = self.func2.im_func.func_code
7.
8.     def __del__(self):
9.         del self.caller1
10.         del self.caller2
11.
12.     def common_func(self):
13.         caller = inspect.currentframe().f_back.f_code
14.         if caller == self.caller1:
15.             print "called by caller 1"
16.         elif caller == self.caller2:
17.             print "called by caller 2"
18.
19.     def func1(self):
20.         self.common_func()
21.
22.     def func2(self):
23.         self.common_func()
This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.
Jul 17 '07 #3
bartonc
6,596 Expert 4TB
If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:
Expand|Select|Wrap|Line Numbers
1. import inspect
2.
3. class FunctionOwner:
4.     def __init__(self):
5.         self.caller1 = self.func1.im_func.func_code
6.         self.caller2 = self.func2.im_func.func_code
7.
8.     def __del__(self):
9.         del self.caller1
10.         del self.caller2
11.
12.     def common_func(self):
13.         caller = inspect.currentframe().f_back.f_code
14.         if caller == self.caller1:
15.             print "called by caller 1"
16.         elif caller == self.caller2:
17.             print "called by caller 2"
18.
19.     def func1(self):
20.         self.common_func()
21.
22.     def func2(self):
23.         self.common_func()
This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.
Well, I think it's kind of cool, even if no one else does.
Jul 18 '07 #4
Thanks for the tip. But what if I don't know the name of function that can call common_func. It could be any arbitrary function name defined by the user who calls common_func.

Thanks

If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:
Expand|Select|Wrap|Line Numbers
1. import inspect
2.
3. class FunctionOwner:
4.     def __init__(self):
5.         self.caller1 = self.func1.im_func.func_code
6.         self.caller2 = self.func2.im_func.func_code
7.
8.     def __del__(self):
9.         del self.caller1
10.         del self.caller2
11.
12.     def common_func(self):
13.         caller = inspect.currentframe().f_back.f_code
14.         if caller == self.caller1:
15.             print "called by caller 1"
16.         elif caller == self.caller2:
17.             print "called by caller 2"
18.
19.     def func1(self):
20.         self.common_func()
21.
22.     def func2(self):
23.         self.common_func()
This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.
Jul 18 '07 #5
Was playing around with bartonc's tip and found out the answer.
Expand|Select|Wrap|Line Numbers
1. caller = inspect.currentframe().f_back.f_code
2. print "I was called by " , caller.co_name
3.
Thanks,
-V
Jul 18 '07 #6
bartonc
6,596 Expert 4TB
Was playing around with bartonc's tip and found out the answer.
Expand|Select|Wrap|Line Numbers
1. caller = inspect.currentframe().f_back.f_code
2. print "I was called by " , caller.co_name
3.
Thanks,
-V
Thanks. Sorry that I missed that part. Glad you found your way through the docs!
Jul 18 '07 #7
Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubModule.Foo()" not just "called by Foo()"
I think I saw the file path for the module in there but I don't want to have to try to determine the submodules from the path if possible.

Cheers,
Pete
Aug 13 '07 #8
bartonc
6,596 Expert 4TB
Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubModule.Foo()" not just "called by Foo()"
I think I saw the file path for the module in there but I don't want to have to try to determine the submodules from the path if possible.

Cheers,
Pete
Hi Pete. Building on the example above:
Expand|Select|Wrap|Line Numbers
1. #.....
2.         print inspect.getfile(caller)
There are others, as well. The docs are here.

Have fun!
Aug 14 '07 #9