Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created July 21, 2018 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypetrella/6f3df40034aad48591d7f28d1c7697ff to your computer and use it in GitHub Desktop.
Save andypetrella/6f3df40034aad48591d7f28d1c7697ff to your computer and use it in GitHub Desktop.
How to reference a scala object in python using Py4j (JavaGateway)
# This function takes the fully classified object name, say: <package>.<name>.
# Returns the static object instance on the heap
def ref_scala_object(object_name):
clazz = jvm.java.lang.Class.forName(object_name+"$")
ff = clazz.getDeclaredField("MODULE$")
o = ff.get(None)
return o
scala_none = ref_scala_object("scala.None")
scala_none.getClass().getName() # returns 'scala.None$'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment