On 6/23/06, Diez B. Roggisch <deets@web.de> wrote:
> I think you mean
>    getattr(target, "someMethod")(10)
> but still a great example of how Jython reflection is simpler than Java's.

No I didn't. Your version is needed in case where I want to call a method that
I didn't know the name beforehand. But _if_ I know the name, the "usual"
syntax works.

One could argue that in such cases one wouldn't use reflection in Java at all
- but instead cast to the desired interface.

Yet I assumed the example to work in something that is supposed to be a
scripting-sort of thing - using reflection in java to gain flexibility. But
then jython is way better - as we certainly both agree :)


Well, I have a good use-case for that then...(stumbled across it recently while doing pydev):

...
        if(node instanceof aliasType){
            aliasType type = (aliasType) node;
            return ((NameTok)type.name).id;
        }
        if(node instanceof ClassDef){
            ClassDef def = (ClassDef) node;
            return ((NameTok)def.name).id;
        }
        if(node instanceof FunctionDef){
            FunctionDef def = (FunctionDef) node;
            return ((NameTok)def.name).id;
        }
...

instead of
...
    if hasattr(node, "name"):
       return node.name.id

I could use the reflection for the java version (I have some libraries that I use to do: REF.hasAttr() and REF.getAttr() in java, so, it might not be so bad either:

if REF.hasAttr(node,"name"):
   return REF.getAttr(REF.getAttr(node,"name"), "id");

But it still it's not the same -- I actually had that version, but as it turned up to be one of the most called methods in pydev, I had to keep the 1st version because that approach turned out to be much slower in the recent profiling efforts in pydev ;-).

Cheers,

Fabio