Jython is an implementation of Python 2.7 written in 100% Pure Java, providing seamless integration with the Java platform and ecosystem.
—
Jython implements the complete Python object system in Java, providing all built-in Python types and the ability to create and manipulate Python objects from Java code.
The central factory class for creating Python objects and accessing Python built-ins.
public final class Py {
// Object creation
public static PyInteger newInteger(int i);
public static PyInteger newInteger(long i);
public static PyLong newLong(String s);
public static PyLong newLong(java.math.BigInteger i);
public static PyLong newLong(int i);
public static PyLong newLong(long l);
public static PyFloat newFloat(float v);
public static PyFloat newFloat(double v);
public static PyComplex newImaginary(double v);
public static PyString newString(char c);
public static PyString newString(String s);
public static PyString newStringOrUnicode(String s);
public static PyUnicode newUnicode(String s);
public static PyBoolean newBoolean(boolean t);
public static PyList newList();
public static PyList newList(PyObject[] elements);
public static PyTuple newTuple(PyObject... elements);
public static PyDictionary newDict();
public static PyStringMap newStringMap();
// Special objects
public static PyObject newDate(Date date);
public static PyObject newTime(Time time);
public static PyObject newDatetime(Timestamp timestamp);
public static PyObject newDecimal(String decimal);
// Type conversion
public static PyObject java2py(Object o);
public static PyString java2py(String s);
public static PyInteger java2py(int i);
public static PyFloat java2py(double d);
public static PyBoolean java2py(boolean b);
}All Python objects in Jython inherit from PyObject, which implements the complete Python object protocol.
public class PyObject implements Serializable {
// Type operations
public PyType getType();
public boolean isCallable();
public boolean isSequenceType();
public boolean isMappingType();
public boolean isNumberType();
// Java conversion
public Object __tojava__(Class<?> c);
public String toString();
public int hashCode();
public boolean equals(Object o);
// Python operations
public PyObject __call__(PyObject args[]);
public PyObject __call__(PyObject args[], String keywords[]);
public PyObject __getattr__(String name);
public void __setattr__(String name, PyObject value);
public void __delattr__(String name);
public PyObject __getitem__(PyObject key);
public void __setitem__(PyObject key, PyObject value);
public void __delitem__(PyObject key);
// Iterator protocol
public PyObject __iter__();
public PyObject __iternext__();
// String representation
public String __str__();
public String __repr__();
// Numeric operations
public PyObject __add__(PyObject other);
public PyObject __sub__(PyObject other);
public PyObject __mul__(PyObject other);
public PyObject __div__(PyObject other);
public PyObject __mod__(PyObject other);
public PyObject __pow__(PyObject other);
public PyObject __and__(PyObject other);
public PyObject __or__(PyObject other);
public PyObject __xor__(PyObject other);
public PyObject __lshift__(PyObject other);
public PyObject __rshift__(PyObject other);
// Comparison operations
public PyObject __lt__(PyObject other);
public PyObject __le__(PyObject other);
public PyObject __gt__(PyObject other);
public PyObject __ge__(PyObject other);
public PyObject __eq__(PyObject other);
public PyObject __ne__(PyObject other);
// Unary operations
public PyObject __neg__();
public PyObject __pos__();
public PyObject __abs__();
public PyObject __invert__();
// Container operations
public int __len__();
public boolean __contains__(PyObject item);
// Context manager protocol
public PyObject __enter__();
public boolean __exit__(PyObject type, PyObject value, PyObject traceback);
}public class PyString extends PyBaseString implements BufferProtocol {
public PyString(String string);
public PyString(char c);
// Access methods
public String getString();
public char charAt(int index);
public int length();
// String operations
public PyString __add__(PyObject other);
public PyString __mul__(PyObject other);
public PyList split();
public PyList split(String sep);
public PyList split(String sep, int maxsplit);
public PyString strip();
public PyString strip(String chars);
public PyString lstrip();
public PyString lstrip(String chars);
public PyString rstrip();
public PyString rstrip(String chars);
public PyString lower();
public PyString upper();
public PyString capitalize();
public PyString title();
public PyString swapcase();
public PyString replace(PyObject oldPiece, PyObject newPiece);
public PyString replace(PyObject oldPiece, PyObject newPiece, int count);
public boolean startswith(PyObject prefix);
public boolean endswith(PyObject suffix);
public int find(PyObject sub);
public int find(PyObject sub, int start);
public int find(PyObject sub, int start, int end);
public int count(PyObject sub);
public PyString join(PyObject seq);
}public class PyUnicode extends PyString {
public PyUnicode(String string);
public PyUnicode(char[] chars);
public PyUnicode(int codepoint);
// Unicode-specific methods
public String encode();
public String encode(String encoding);
public String encode(String encoding, String errors);
public boolean isalnum();
public boolean isalpha();
public boolean isdecimal();
public boolean isdigit();
public boolean islower();
public boolean isnumeric();
public boolean isspace();
public boolean istitle();
public boolean isupper();
}public class PyInteger extends PyObject {
public PyInteger(int value);
// Access methods
public int getValue();
public int asInt();
public long asLong();
public double asDouble();
// Arithmetic operations inherited from PyObject
// __add__, __sub__, __mul__, __div__, etc.
}public class PyLong extends PyObject {
public PyLong(long value);
public PyLong(BigInteger value);
public PyLong(String s);
public PyLong(String s, int radix);
// Access methods
public BigInteger getValue();
public long asLong();
public int asInt();
public double asDouble();
// Bit operations
public int bit_length();
public PyLong conjugate();
}public class PyFloat extends PyObject {
public PyFloat(double value);
public PyFloat(float value);
// Access methods
public double getValue();
public double asDouble();
public float asFloat();
public int asInt();
// Float-specific methods
public boolean is_integer();
public PyTuple as_integer_ratio();
public String hex();
public static PyFloat fromhex(String s);
}public class PyComplex extends PyObject {
public PyComplex(double real);
public PyComplex(double real, double imag);
// Access methods
public double getReal();
public double getImag();
public PyFloat real;
public PyFloat imag;
// Complex operations
public PyComplex conjugate();
public double __abs__();
}public class PyList extends PySequenceList {
public PyList();
public PyList(PyObject[] elements);
public PyList(Collection<?> c);
// List operations
public void append(PyObject o);
public void insert(int index, PyObject o);
public PyObject pop();
public PyObject pop(int index);
public void remove(PyObject o);
public void reverse();
public void sort();
public void sort(PyObject cmp);
public void sort(PyObject cmp, PyObject key);
public void sort(PyObject cmp, PyObject key, PyObject reverse);
public int count(PyObject o);
public int index(PyObject o);
public int index(PyObject o, int start);
public int index(PyObject o, int start, int stop);
public void extend(PyObject o);
// Sequence operations inherited from PySequenceList
public int __len__();
public PyObject __getitem__(int index);
public void __setitem__(int index, PyObject value);
public void __delitem__(int index);
public PyList __getslice__(int start, int stop, int step);
}public class PyTuple extends PySequenceList {
public PyTuple();
public PyTuple(PyObject[] elements);
public PyTuple(Collection<?> c);
// Tuple is immutable - no modification methods
// Sequence operations inherited from PySequenceList
public int __len__();
public PyObject __getitem__(int index);
public PyTuple __getslice__(int start, int stop, int step);
public int count(PyObject o);
public int index(PyObject o);
}public class PyDictionary extends AbstractDict implements ConcurrentMap {
public PyDictionary();
public PyDictionary(Map<PyObject, PyObject> map);
public PyDictionary(PyObject[] elements);
// Dictionary operations
public PyObject get(PyObject key);
public PyObject get(PyObject key, PyObject defaultValue);
public void put(PyObject key, PyObject value);
public PyObject pop(PyObject key);
public PyObject pop(PyObject key, PyObject defaultValue);
public PyObject popitem();
public void clear();
public void update(PyObject other);
public void update(PyObject[] args, String[] keywords);
// View operations
public PyList keys();
public PyList values();
public PyList items();
public PyObject iterkeys();
public PyObject itervalues();
public PyObject iteritems();
// Dictionary methods
public PyObject setdefault(PyObject key);
public PyObject setdefault(PyObject key, PyObject defaultValue);
public boolean has_key(PyObject key);
public PyDictionary copy();
}public class PySet extends BaseSet {
public PySet();
public PySet(PyObject[] elements);
public PySet(PyObject iterable);
// Mutable set operations
public void add(PyObject o);
public void remove(PyObject o);
public void discard(PyObject o);
public PyObject pop();
public void clear();
public void update(PyObject other);
public void intersection_update(PyObject other);
public void difference_update(PyObject other);
public void symmetric_difference_update(PyObject other);
}
public class PyFrozenSet extends BaseSet {
public PyFrozenSet();
public PyFrozenSet(PyObject[] elements);
public PyFrozenSet(PyObject iterable);
// Immutable - no modification methods
// Set operations return new sets
public PyObject union(PyObject other);
public PyObject intersection(PyObject other);
public PyObject difference(PyObject other);
public PyObject symmetric_difference(PyObject other);
}// Using factory methods
PyString str = Py.newString("Hello, World!");
PyInteger num = Py.newInteger(42);
PyList list = Py.newList();
PyDictionary dict = Py.newDict();
// Using constructors
PyString str2 = new PyString("Another string");
PyFloat pi = new PyFloat(3.14159);
PyTuple tuple = new PyTuple(new PyObject[]{str, num, pi});// Working with lists
PyList numbers = Py.newList();
numbers.append(Py.newInteger(1));
numbers.append(Py.newInteger(2));
numbers.append(Py.newInteger(3));
PyObject first = numbers.__getitem__(Py.newInteger(0));
System.out.println(first); // 1
// Working with dictionaries
PyDictionary person = Py.newDict();
person.put(Py.newString("name"), Py.newString("Alice"));
person.put(Py.newString("age"), Py.newInteger(30));
PyObject name = person.get(Py.newString("name"));
System.out.println(name); // AlicePyString greeting = Py.newString("Hello, World!");
PyString upper = greeting.upper();
System.out.println(upper); // HELLO, WORLD!
PyList words = greeting.split(Py.newString(", "));
System.out.println(words); // ['Hello', 'World!']
PyString joined = Py.newString(" | ").join(words);
System.out.println(joined); // Hello | World!PyInteger a = Py.newInteger(10);
PyInteger b = Py.newInteger(3);
PyObject sum = a.__add__(b);
PyObject diff = a.__sub__(b);
PyObject prod = a.__mul__(b);
PyObject quot = a.__div__(b);
System.out.println(sum); // 13
System.out.println(diff); // 7
System.out.println(prod); // 30
System.out.println(quot); // 3PyObject obj = Py.newString("123");
// Type checking
if (obj instanceof PyString) {
System.out.println("It's a string");
}
// Convert to Java types
String javaStr = obj.toString();
int javaInt = Integer.parseInt(javaStr);
// Convert back to Python
PyInteger pyInt = Py.newInteger(javaInt);The Py class provides common Python constants:
public final class Py {
public static final PyObject None;
public static final PyObject Ellipsis;
public static final PyObject NotImplemented;
public static final String[] NoKeywords;
public static final PyObject[] EmptyObjects;
public static final PyFrozenSet EmptyFrozenSet;
public static final PyTuple EmptyTuple;
public static final PyInteger Zero;
public static final PyInteger One;
public static final PyBoolean False;
public static final PyBoolean True;
public static final PyString EmptyString;
public static final PyUnicode EmptyUnicode;
public static final PyString Newline;
public static final PyUnicode UnicodeNewline;
public static final PyString Space;
public static final PyUnicode UnicodeSpace;
}// Using constants
PyObject result = condition ? Py.True : Py.False;
PyList emptyList = Py.newList(Py.EmptyObjects);
PyTuple emptyTuple = Py.EmptyTuple;
// None comparisons
if (value == Py.None) {
System.out.println("Value is None");
}Install with Tessl CLI
npx tessl i tessl/maven-org-python--jython-standalone