From: Samuele P. <pe...@in...> - 2001-09-19 17:36:44
|
Hi. Mark Robinson wrote [on comp.lang.python]: > I hope it isn't considered inappropriate to post jython questions here! No, but if you're question is jython related (e.g. related to java integration) and not a python general question, you are better served by jyt...@li... > > I am running jython 2.1a1 on winNT4 and getting a very unusual error. In > responce to a getFocus event I attempt to determine which object > obtained the focus (in this case a JTextField object) as follows: > > e.getComponent().getClass().getName() > > At run time I am getting a TypeError exception saying that getName() > expects 1 arg and is receiving 0. From the Java documentation it would > seem that getName() doesn't ever take an argument. > Can anyone tell me if this is implemented differently in jython or if I > am just making a daft mistake, or where I might be able to find out what > argument it is expecting (my searching has been fruitless). > That's tricky. Let's see >>> from java.io import File >>> from java.lang import Object >>> Object.getName() 'java.lang.Object' >>> File.getName() Traceback (innermost last): File "<console>", line 1, in ? TypeError: getName(): expected 1 args; got 0 >>> # ??? That's the very same problem. Why does this happen? The contents of File.__dict__ masks the contents File.__class__ .__dict__ , File.__class__ is java.lang.Class but File has a getName method too, so File.getName give you an unbound version of that method, that expects a File argument (the this/self argument): >>> f=File('/usr') >>> File.getName(f) 'usr' In general when you want to call a method of java.lang.Class on a Class instance and avoid this kind of clash, you should use the unbound version: >>> from java.lang import Class >>> Class.getName(File) 'java.io.File' Or using the pythonic classes protocol, which has an equivalent support at least for getName: >>> File.__name__ 'java.io.File' You encounter the problem because java.awt.Component has a getName method. regards, Samuele Pedroni. |