Skip to content
bsteffensmeier edited this page Jan 27, 2016 · 4 revisions

Python Keywords

It is impossible to directly access java methods and fields from python if the name is the same as a python keyword. For example print is a keyword in python so the python interpreter will not allow object attributes named print so the following code will not work as expected:

    from java.lang import System
    System.out.print('Hello')

You can work around this limitation by using getattr to access the print method like this:

    from java.lang import System
    getattr(System.out, 'print')('Hello')

Here is all the keywords currently reserved by python, all of these keyword will have the same problems if they are used as field or method names on java objects.

    and       del       from      not       while
    as        elif      global    or        with
    assert    else      if        pass      yield
    break     except    import    print
    class     exec      in        raise
    continue  finally   is        return 
    def       for       lambda    try

Python Packages

When a java package and a python module have the same prefix then you will not be able to use both from within the same jep interpreter. For example it is not possible to use both the io.netty java package and the io module from the same interpreter. This behavior is controlled by the ClassEnquirer which is passed into jep. The default ClassEnquirer will allow any java packages on the classpath to override python packages. Beginning in jep 3.5 the default ClassEnquirer will ignore java packages that start with io and re since these are common python modules and uncommon java prefixes.

Clone this wiki locally