In CPython, you can check if something is, for example, a list
, using isinstance(something, list)
.
This same code doesn't work in Jython, though. In Jython, it throws this exception: TypeError: isinstance(): 2nd arg is not a class
.
In Jython, list
is a function
, not a class
. You can find this out by running: list.__class__
, which will print <jclass org.python.core.BuiltinFunctions at ...>
in contrast to Python which will print out <type 'list'>
given the same code.
I found two ways to make this work.
isinstance(something, [].__class__)
.[].__class__
in Jython, it'll print out <jclass org.python.core.PyList at ...>
. So you could do import org
and then isinstance(something, org.python.core.PyList)
.Neither option looks all that great. The second one is probably easier to understand, but has the disadvantage of only working on Jython, whereas the first one works equally well on both runtimes.