Archive for the ‘Notes’ Category

Python and Jython: they’re the main two

January 22, 2008

I’ve come across some differences between python and jython while amending a grinder test harness. Using python 2.5.1 I can do this to get a uri embedded in a string:

tup = text.partition('rdf:about="')
resourceTup = tup[2].partition('"')
print resourceTup[0]

partition returns an array of substring before the separator, the separator itself, and the substring after the separator.

Of course, when running under jython, it would error with:
‘string’ object has no attribute ‘partition’

But this works in jython:

tup = text.split('rdf:about="')
resourceTup = tup[1].split('"')
print resourceTup[0]

I wondered if jython were using the java String class as you can use split and join, however, there is no indexOf method etc as there is in the java String class.

Here is a useful command to find out what methods an object supports

dir(str)

and can be used in jython to find out what methods a Java class supports

dir(String.class)

There are of course far more fundamental differences between the two than itty bitty string handling.

Advertisements