Leo's Technical Blog

Jython: How to Instantiate Classes Written in Python Code

Introduction

user

Leo Soto


python, jython

Jython: How to Instantiate Classes Written in Python Code

Posted by Leo Soto on .
Featured

python, jython

Jython: How to Instantiate Classes Written in Python Code

Posted by Leo Soto on .

This week, as part of my GSoC project, I had to do some work related with zxJDBC, the very cool DBAPI <-> JDBC brige which is bundled with Jython. Among other things, I'm improving the default type mapping, adding support for converting java.sql.* instances to pythonic datetime.* ones.

As the type mapping is written inside a class written in Java, I was confronted to the problem of how to instantiate datetime.* objects, which (by now) Jython implements using pure python code. The answer was very simple: Just do by hand what Python always does when you write:

  
import datetime  
datetime.date(year, month, day)  

You know, the import statement is implemented by the import builtin, foo.bar is getattr(foo, 'bar') and f(x) is f.call(x). Then, the following is equivalent to the previous snippet:

datetime = __import__('datetime')
getattr(datetime, 'date').__call__(year, month, day)

Which, translated to Java/Jython looks almost the same:

  
PyObject datetime = __builtin__.__import__('datetime')  
datetime.__getattr__('date').__call__(Py.newInt(year),  
                                      Py.newInt(month), 
                                      Py.newInt(day))

Once you get the idea, not only instantiating, but doing anything with classes written with Jython from Java code looks like a piece of cake.

Hey!, How easiest could it be?

Note that this works if all the Jython machinery is in place. This will be the case if your Java code is being called (directly or indirectly) from python code being ran on Jython. As any Jython module, such as zxJDBC, is always used from python code, this is something to have in mind when writing them in Java.