473,583 Members | 3,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Names on a function call

20 New Member
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 1768
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Expert
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 Recognized Expert Expert
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
indiarocks
20 New Member
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
indiarocks
20 New Member
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 Recognized Expert Expert
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
MisterPete
5 New Member
Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubMod ule.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 Recognized Expert Expert
Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubMod ule.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
1493
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 lower_case_with_underscores for rarely-used utility functions. Now it says to use low_case_with_underscores for everything, but it claims to be last updated...
8
4821
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
404
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 own shared object / dll code which will link with my large ugly program. Problem: naming collisions. If my program has: void...
3
2314
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 point it to me. Direct question: How can I know if a function is defined in more than one static library that I am using to link ?
2
1539
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 be done by mapping string function names with functions themselves? Is there a better and direct way? Thanks in advance!
3
1302
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 to see the function names in 3C chapter, how should I do it? I know "man sqrt" will give me the information about the function sqrt. But there...
7
2117
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 function names from the file, so the functions can be executed in the order the function names are stored in a file. While trying to read the names...
5
2941
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 a+b
4
2111
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
3265
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
7893
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8188
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.