473,804 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_nam e
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 5404
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_nam e
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*******@yaho o.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_nam e
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
31668
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 coffee(); => should give me 'milk'
7
4468
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
1855
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 access a method of Class2 (say objC2.func1(); Now in function 'func1()' of Class2 I wanna get the Caller class name ('Class1' Plz don say me to pass the name of class as an arg in 'func()'; i need som other way ... this is coz Class2 is a central...
9
6001
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. I can get the calling assembly rather easily, but let's say within Assembly X I have class A
7
1720
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 don't need to be handled, but some must be handled by the program that uses the class. I'm looking for something along the lines of Class MyClass Event OptionalEvent(parms) Event RequiredEvent(parms)
1
366
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 to know, which caller class invoke my existing class method. Any help is much appreciated.
13
20780
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 & I don't want to copy + paste code. How can I inherit those member variables? Thanks
16
1374
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) So that:
7
2691
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
9716
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.