473,320 Members | 1,879 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,320 software developers and data experts.

Retrieve class name of caller

For debugging/logging purposes, I have a function

def Log(msg):
print '%s.%s: %s' % (cls, method, msg)

I call this function from a class method, and I would like to retrieve
the name of the caller method and its class. I already came up with

method = sys._getframe(1).f_code.co_name
cls = sys._getframe(1).f_locals['self'].__class__

I wonder if there are better ways to retrieve this info. In
particular, my implementation assumes that the instance parameter of
the method is called 'self', which it usually is, of course.

Regards,
Koen.
Jul 18 '05 #1
2 5168
Koen Van Herck wrote:
For debugging/logging purposes, I have a function

def Log(msg):
print '%s.%s: %s' % (cls, method, msg)

I call this function from a class method, and I would like to retrieve
the name of the caller method and its class. I already came up with

method = sys._getframe(1).f_code.co_name
cls = sys._getframe(1).f_locals['self'].__class__

I wonder if there are better ways to retrieve this info. In
particular, my implementation assumes that the instance parameter of
the method is called 'self', which it usually is, of course.

Regards,
Koen.


Take look to the inspect module. Could be what you need (and even more).

Jul 18 '05 #2
Bertrand Geston <be*******@yahoo.fr> wrote in message news:<bk************@sinclair.be.wanadoo.com>...
Koen Van Herck wrote:
For debugging/logging purposes, I have a function

def Log(msg):
print '%s.%s: %s' % (cls, method, msg)

I call this function from a class method, and I would like to retrieve
the name of the caller method and its class. I already came up with

method = sys._getframe(1).f_code.co_name
cls = sys._getframe(1).f_locals['self'].__class__

I wonder if there are better ways to retrieve this info. In
particular, my implementation assumes that the instance parameter of
the method is called 'self', which it usually is, of course.

Regards,
Koen.


Take look to the inspect module. Could be what you need (and even more).


OK. But if I have the frame or code object, how can I get the method
or function object? For example, for a method m I have code object c:

c = m.im_func.func_code

How can I find m from c ?
Jul 18 '05 #3

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

Similar topics

17
by: warteschlange | last post by:
is there a way to find out the object/function name from inside object/function. function coffee(){ alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee' } var milk = new...
7
by: Tamir Khason | last post by:
I have a class public class Foo { public Foo() { DoSomething() } } I want to be able to do something else while the class enters GC - no more
4
by: Hmnt | last post by:
Hi Hope ur smart enough to get what i wanna ask ... (from the sub line Brief Explaination I've 2 classas Class1 and Class Now i've an instance of Class2 ( Class2 objC2 = new Class2(); Next i...
9
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function....
7
by: Michael D. Ober | last post by:
Is there anyway to raise an event from a class and require that any program using that class (not just inheritance) have an event handler for specific events in the class? In my case, some events...
1
by: qingxun | last post by:
Hi! Is there a way in VB.NET to determine the caller's class name? System.Reflection.MethodBase.GetCurrentMethod.GetType basically telling me what is my current executing class, but I wanted...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
16
by: Donn Ingle | last post by:
Hello, Is there a way I can, for debugging, access the instance variable name from within a class? E.g: Class X: def debug(self): print "My instance var is %s" % (some magic Python stuff) ...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.