473,387 Members | 3,033 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,387 software developers and data experts.

Identifying Caller

Is there a way to identify the caller of a function? For example:

def foo():
print <Caller Functions name>

def bar():
foo()

I want foo to print "bar"... And it will be great if I can also detect
call is came from which module.

Thanks.

--
Love, Respect, Linux
################################################## ##########################
"And the next time you consider complaining that running Lucid Emacs
19.05 via NFS from a remote Linux machine in Paraguay doesn't seem to
get the background colors right, you'll know who to thank."
(By Matt Welsh)
################################################## ##########################
Tonguç Yumruk

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/iR6d1xWu4MLSyoYRAoE4AJ4r1+kwxqnFOr+Mu9QOqDGF4Pw6KQ Cg3Syq
jabkOdMQup66Xymjtux1QXY=
=XtrV
-----END PGP SIGNATURE-----

Jul 18 '05 #1
2 5728
In article <ma************************************@python.org >, Tonguç
Yumruk <tr*****@ttnet.net.tr> writes
Is there a way to identify the caller of a function? For example:

def foo():
print <Caller Functions name>

def bar():
foo()

I want foo to print "bar"... And it will be great if I can also detect
call is came from which module.

Thanks.

try something like this

#############################
def who_called_me(n=0):
import sys
f = sys._getframe(n)
c = f.f_code
return c.co_filename, c.co_name, f.f_lineno

if __name__=='__main__':
def test2():
print 'test2',who_called_me(1)

def test1():
print 'test1',who_called_me(1)
test2()

def test():
print 'test',who_called_me(1)
test1()
test2()

class dingo:
def __init__(self):
self.a = 1
print '__init__',who_called_me(1)
def doit(self):
print 'dingo.doit',who_called_me(1), self.a
def calldoit(self):
print 'dingo.calldoit',who_called_me(1), self.a
self.doit()
test()
def mongo():
print 'mongo', who_called_me(1)
d=dingo()
d.doit()
d.calldoit()
test()
test1()
test2()
mongo()
--
Robin Becker
Jul 18 '05 #2
Tonguç Yumruk wrote:
Is there a way to identify the caller of a function? For example:

def foo():
print <Caller Functions name>

def bar():
foo()

I want foo to print "bar"... And it will be great if I can also detect
call is came from which module.


See function _getframe in module sys. The frame object that
it returns has many attributes useful for your tasks. In particular,
sys._getframe(1).f_code.co_name should be 'bar' as you require,
and sys._getframe(1).f_globals['__name__'] should be the module
name which you desire.
Alex

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David | last post by:
I know that identifying the user IP address with HTTP_SERVER_VARS; is reliant on the browser agent but I have stumpled upon the following code which I have tried to understand but failed! ...
7
by: ivan_oj28 | last post by:
Hi, I am developing an application where I need to read the caller id from an incoming call. The only info (for now) I need is the caller id info, so that I can display the propper caller info on...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
8
by: Simon Harvey | last post by:
Hi, Does anyone know an easy way to identify the exceptions that a class can potentially throw. I was looking for a list of the exceptions that the Hashtable class can potentially provide,...
9
by: jaden10001 | last post by:
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there a way to get the name of the caller of a function in Opera 7?
4
by: Thomas Mlynarczyk | last post by:
Hi, I stumbled over a strange behaviour of Mozilla. When I want to access the caller property of a function that was not called from within another function, Mozilla seems to abort the script....
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
7
by: Rahul | last post by:
Hi Everyone, I have a function utility() which is a part of a library and i would like to know as to who is invoking the function. I could request that information as a parameter enumeration,...
7
by: =?UTF-8?B?QW50w7NuaW8gTWFycXVlcw==?= | last post by:
Hi, Sorry if this's been discussed before, I couldn't find it. As well you know, the ECMAScript standard doesn't include any way to access a function's caller. This has been available on Mozilla...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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...

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.