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?

2
  • 2
    have you seen stackoverflow.com/questions/44623470/… Commented Jul 31, 2018 at 0: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 Commented Jul 31, 2018 at 21:04

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.