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
8 1755
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: -
def common_func(call_func):
-
if call_func == 1:
-
# called from func1
-
if call_func == 2:
-
# called from func2
-
-
def func1():
-
common_func(1)
-
-
def func2():
-
common_func(2)
-
Why do you need that?
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: - import inspect
-
-
class FunctionOwner:
-
def __init__(self):
-
self.caller1 = self.func1.im_func.func_code
-
self.caller2 = self.func2.im_func.func_code
-
-
def __del__(self):
-
del self.caller1
-
del self.caller2
-
-
def common_func(self):
-
caller = inspect.currentframe().f_back.f_code
-
if caller == self.caller1:
-
print "called by caller 1"
-
elif caller == self.caller2:
-
print "called by caller 2"
-
-
def func1(self):
-
self.common_func()
-
-
def func2(self):
-
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.
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: - import inspect
-
-
class FunctionOwner:
-
def __init__(self):
-
self.caller1 = self.func1.im_func.func_code
-
self.caller2 = self.func2.im_func.func_code
-
-
def __del__(self):
-
del self.caller1
-
del self.caller2
-
-
def common_func(self):
-
caller = inspect.currentframe().f_back.f_code
-
if caller == self.caller1:
-
print "called by caller 1"
-
elif caller == self.caller2:
-
print "called by caller 2"
-
-
def func1(self):
-
self.common_func()
-
-
def func2(self):
-
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.
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: - import inspect
-
-
class FunctionOwner:
-
def __init__(self):
-
self.caller1 = self.func1.im_func.func_code
-
self.caller2 = self.func2.im_func.func_code
-
-
def __del__(self):
-
del self.caller1
-
del self.caller2
-
-
def common_func(self):
-
caller = inspect.currentframe().f_back.f_code
-
if caller == self.caller1:
-
print "called by caller 1"
-
elif caller == self.caller2:
-
print "called by caller 2"
-
-
def func1(self):
-
self.common_func()
-
-
def func2(self):
-
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.
Was playing around with bartonc's tip and found out the answer. -
caller = inspect.currentframe().f_back.f_code
-
print "I was called by " , caller.co_name
-
Thanks,
-V
Was playing around with bartonc's tip and found out the answer. -
caller = inspect.currentframe().f_back.f_code
-
print "I was called by " , caller.co_name
-
Thanks,
-V
Thanks. Sorry that I missed that part. Glad you found your way through the docs!
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
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: - #.....
-
print inspect.getfile(caller)
There are others, as well. The docs are here.
Have fun!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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");
}
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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){
|
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.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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,...
| |