469,332 Members | 6,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

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 1510
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Leif K-Brooks | last post: by
8 posts views Thread by Falc2199 | last post: by
3 posts views Thread by joseluismarchetti | last post: by
2 posts views Thread by newhand | last post: by
3 posts views Thread by Xiaoshen Li | last post: by
7 posts views Thread by Petr Jakes | last post: by
16 posts views Thread by Xiaoxiao | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.