0

I'm working in Python and trying to call some Java / Scala libraries using Jython. One Scala library I'm using has singleton objects, which can be called from Java like this: (see this answer)

Person$.MODULE$

I can't do this in my code because the $ sign is a syntax error in Python:

person = Person.MODULE$.apply()
                     ^
SyntaxError: no viable alternative at character '$'

How can I access this object from Python / Jython?

Joe Malt
  • 387
  • 1
  • 15
  • 2
    have you seen https://stackoverflow.com/questions/44623470/how-do-i-import-a-java-classname-with-a-dollar-sign-or-refer-to-a-field-or – joel Jul 31 '18 at 00:22
  • @JoelBerkeley thanks for linking to that - I'm wondering whether there's another way of accessing a singleton object that doesn't involve any dollar signs – Joe Malt Jul 31 '18 at 21:04

1 Answers1

0

I found a solution using reflection. The name of the class with the $ sign is provided as a string literal.

from java.lang import Class

person = Class.forName("full.path.to.Person$").getField("MODULE$").get(None)
Joe Malt
  • 387
  • 1
  • 15