introspection?

Jim Benson jbenson at lowell.edu
Wed Aug 18 19:32:58 EDT 2004


On Wed, 18 Aug 2004, Phil Frost wrote:

> >>> class C:
> ...  pass
> >>> C().__class__.__name__
> 'C'
> 
> That is, __class__ of an instance gives you the class object of which
> it's an instance, and __name__ gives you the identifier that represents
> the class. Classes also have a __module__ attribute.
> 

Thank you Phil and Troy for pointing out that __class__.__name__
gives me the class name. How do i get the method name?
i.e i could now modify my example as:

class JJTest:

	def methodA(self):

        	print 'ERROR: in JJTest.methodA'
		s1 = self.__class__.__name__ 
               	s2 = 'methodA'
                s = s1 + '.' + s2
                print 'ERROR: in %s' s   
	 
>>> from jjtest import JJTest
>>> jj = JJTest()
>>> jj.methodA()
ERROR: in JJTest.methodA
ERROR: in JJTest.methodA
>>>

how do i get s2?

Thanks,

Jim	



> On Wed, Aug 18, 2004 at 02:43:46PM -0700, Jim Benson wrote:
> > 
> > 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
> 





More information about the Python-list mailing list