Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   introspection? (http://www.velocityreviews.com/forums/t334572-introspection.html)

Jim Benson 08-18-2004 09:43 PM

introspection?
 

newbie question here:

How does one get the class and method name from within
a method? i.e here i have obviously hardcoded the class
and method name:

class JJTest:

def methodA(self):

print 'ERROR: in JJTest.methodA'

>>> from jjtest import JJTest
>>> jj = JJTest()
>>> jj.methodA()

ERROR: in JJTest.methodA
>>>


The Java way of doing this would be something like:

System.out.println("ERROR in: " + this.getClass().getName());

I suspect that there are attributes that one can use to do
something similar in Python.

If any of you Python experts would let me know how to do this in
Python, i would appreciate it.

Thanks,

Jim




Neal Norwitz 08-19-2004 04:09 AM

Re: introspection?
 
Jim Benson <jbenson@lowell.edu> wrote in message news:<mailman.1907.1092865845.5135.python-list@python.org>...
>
> How does one get the class and method name from within
> a method?


>>> import sys
>>> class C:

... def foo(self):
... print self.__class__.__name__, sys._getframe().f_code.co_name
...
>>> C().foo()

C foo

Neal


All times are GMT. The time now is 05:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.