473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Similar topics

9
by: Leif K-Brooks | last post by:
I try to make my code comply to the Python style guide (http://www.python.org/peps/pep-0008.html). Last time I read it, I swear that it said to use CamelCase for often-used functions and...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
12
by: Eric | last post by:
I've got a pretty large C program with global variables and function names strewn about (i.e. no "static" declarations in front of them). Now I want to expose the ability for user's to supply their...
3
by: joseluismarchetti | last post by:
Hello everybody, Although I am sure this is an important question for this group, I am not sure this question belongs to this group and I will be happy to move it to the correct one after you...
2
by: newhand | last post by:
If somehow a bunch of function names in string can be passed into an executible, is it possible that for each name call, it will trigger the corresponding function of that name? Of course, this can...
3
by: Xiaoshen Li | last post by:
Dear All, A tutorial told me that there are more than 700 functions available. To see all the function names, man 3 intro. But on my machine, it only gives one page text. For example, if I hope...
7
by: Petr Jakes | last post by:
I have got names of functions stored in the file. For the simplicity expect one row only with two function names: printFoo, printFOO In my code I would like to define functions and then to read...
5
by: Sakcee | last post by:
python provides a great way of dynamically creating fuctions calls and class names from string a function/class name can be stored as string and called/initilzed e.g def foo(a,b): return...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.