oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.17k stars 101 forks source link

Accessing Super Class Methods of a Java Class in Python #400

Closed RevolvingMadness closed 1 month ago

RevolvingMadness commented 1 month ago

I have a Java class:

public class Foo {
    public void bar() {
        System.out.println("Foo class in Java");
    }
}

I expose the Foo class to Python:

Value bindings = context.getBindings("python");

bindings.putMember("Foo", Foo.class);

I have a simple Python program that runs in GraalPy:

class ExtendedFoo(Foo):
    def bar(self):
        # The errors occurs here.
        # It should print "Foo class in Java"
        super().bar()
        print("Extended Foo class in Python")

foo = Foo()
foo.bar() # "Foo class in Java"

extendedFoo = ExtendedFoo()
extendedFoo.bar() # error

I would like to know if it's even supported and if it's even possible or if I'm doing something wrong.

The error I am currently getting is 'super' object has no attribute 'bar'

msimacek commented 1 month ago

Hi @RevolvingMadness, there is a separate syntax for accessing the Java superclass - instead of super().bar() you need to do self.__super__.bar() (doc).

RevolvingMadness commented 1 month ago

Thank you so much! Maybe I should actually try to look at the docs before opening an issue...