From: Pekka L. <pe...@ik...> - 2007-05-09 13:32:26
|
2007/5/9, Lars Heuer <he...@se...>: > In one method I have to receive a Java class (an interface) and return > a concrete implementation for it. Now, I have the problem, that the > following does not work: > > >>> def class_name(cls): > return cls.getName() > > >>> from somepackge import SomeInterface > >>> class_name(SomeInterface.class) > Traceback (innermost last): > File "<console>", line 1, in ? > File "<console>", line 2, in class_name > TypeError: getName(): expected 1 args; got 0 > > > Is it possible to get the class name? How? > > Class.getName(SomeInterface.class) > > does not work either. Instead of SomeInterface.class.getName() you should either SomeInterface.getName(), SomeInterface.__name__ (perhaps more Pythonic), SomeInterface.class.getName(SomeInterface) or SomeInterface.class.__name__. At least those work with java.lang.Runnable interface as shown below. Jython 2.2b1 on java1.5.0_10 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> from java.lang import Runnable >>> Runnable.getName() 'java.lang.Runnable' >>> Runnable.__name__ 'java.lang.Runnable' >>> Runnable.class.getName() Traceback (innermost last): File "<console>", line 1, in ? TypeError: getName(): expected 1 args; got 0 >>> Runnable.class.getName(Runnable) 'java.lang.Runnable' >>> Runnable.class.__name__ 'java.lang.Class' >>> Cheers, .peke |