or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdexception-handling.mdindex.mdjava-integration.mdjsr223-scripting.mdpython-execution.mdpython-objects.md

python-objects.mddocs/

0

# Python Objects

1

2

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.

3

4

## Object Factory - Py Class

5

6

The central factory class for creating Python objects and accessing Python built-ins.

7

8

```java { .api }

9

public final class Py {

10

// Object creation

11

public static PyInteger newInteger(int i);

12

public static PyInteger newInteger(long i);

13

public static PyLong newLong(String s);

14

public static PyLong newLong(java.math.BigInteger i);

15

public static PyLong newLong(int i);

16

public static PyLong newLong(long l);

17

public static PyFloat newFloat(float v);

18

public static PyFloat newFloat(double v);

19

public static PyComplex newImaginary(double v);

20

public static PyString newString(char c);

21

public static PyString newString(String s);

22

public static PyString newStringOrUnicode(String s);

23

public static PyUnicode newUnicode(String s);

24

public static PyBoolean newBoolean(boolean t);

25

public static PyList newList();

26

public static PyList newList(PyObject[] elements);

27

public static PyTuple newTuple(PyObject... elements);

28

public static PyDictionary newDict();

29

public static PyStringMap newStringMap();

30

31

// Special objects

32

public static PyObject newDate(Date date);

33

public static PyObject newTime(Time time);

34

public static PyObject newDatetime(Timestamp timestamp);

35

public static PyObject newDecimal(String decimal);

36

37

// Type conversion

38

public static PyObject java2py(Object o);

39

public static PyString java2py(String s);

40

public static PyInteger java2py(int i);

41

public static PyFloat java2py(double d);

42

public static PyBoolean java2py(boolean b);

43

}

44

```

45

46

## Base Python Object

47

48

All Python objects in Jython inherit from `PyObject`, which implements the complete Python object protocol.

49

50

```java { .api }

51

public class PyObject implements Serializable {

52

// Type operations

53

public PyType getType();

54

public boolean isCallable();

55

public boolean isSequenceType();

56

public boolean isMappingType();

57

public boolean isNumberType();

58

59

// Java conversion

60

public Object __tojava__(Class<?> c);

61

public String toString();

62

public int hashCode();

63

public boolean equals(Object o);

64

65

// Python operations

66

public PyObject __call__(PyObject args[]);

67

public PyObject __call__(PyObject args[], String keywords[]);

68

public PyObject __getattr__(String name);

69

public void __setattr__(String name, PyObject value);

70

public void __delattr__(String name);

71

public PyObject __getitem__(PyObject key);

72

public void __setitem__(PyObject key, PyObject value);

73

public void __delitem__(PyObject key);

74

75

// Iterator protocol

76

public PyObject __iter__();

77

public PyObject __iternext__();

78

79

// String representation

80

public String __str__();

81

public String __repr__();

82

83

// Numeric operations

84

public PyObject __add__(PyObject other);

85

public PyObject __sub__(PyObject other);

86

public PyObject __mul__(PyObject other);

87

public PyObject __div__(PyObject other);

88

public PyObject __mod__(PyObject other);

89

public PyObject __pow__(PyObject other);

90

public PyObject __and__(PyObject other);

91

public PyObject __or__(PyObject other);

92

public PyObject __xor__(PyObject other);

93

public PyObject __lshift__(PyObject other);

94

public PyObject __rshift__(PyObject other);

95

96

// Comparison operations

97

public PyObject __lt__(PyObject other);

98

public PyObject __le__(PyObject other);

99

public PyObject __gt__(PyObject other);

100

public PyObject __ge__(PyObject other);

101

public PyObject __eq__(PyObject other);

102

public PyObject __ne__(PyObject other);

103

104

// Unary operations

105

public PyObject __neg__();

106

public PyObject __pos__();

107

public PyObject __abs__();

108

public PyObject __invert__();

109

110

// Container operations

111

public int __len__();

112

public boolean __contains__(PyObject item);

113

114

// Context manager protocol

115

public PyObject __enter__();

116

public boolean __exit__(PyObject type, PyObject value, PyObject traceback);

117

}

118

```

119

120

## Built-in Python Types

121

122

### String Types

123

124

#### PyString

125

126

```java { .api }

127

public class PyString extends PyBaseString implements BufferProtocol {

128

public PyString(String string);

129

public PyString(char c);

130

131

// Access methods

132

public String getString();

133

public char charAt(int index);

134

public int length();

135

136

// String operations

137

public PyString __add__(PyObject other);

138

public PyString __mul__(PyObject other);

139

public PyList split();

140

public PyList split(String sep);

141

public PyList split(String sep, int maxsplit);

142

public PyString strip();

143

public PyString strip(String chars);

144

public PyString lstrip();

145

public PyString lstrip(String chars);

146

public PyString rstrip();

147

public PyString rstrip(String chars);

148

public PyString lower();

149

public PyString upper();

150

public PyString capitalize();

151

public PyString title();

152

public PyString swapcase();

153

public PyString replace(PyObject oldPiece, PyObject newPiece);

154

public PyString replace(PyObject oldPiece, PyObject newPiece, int count);

155

public boolean startswith(PyObject prefix);

156

public boolean endswith(PyObject suffix);

157

public int find(PyObject sub);

158

public int find(PyObject sub, int start);

159

public int find(PyObject sub, int start, int end);

160

public int count(PyObject sub);

161

public PyString join(PyObject seq);

162

}

163

```

164

165

#### PyUnicode

166

167

```java { .api }

168

public class PyUnicode extends PyString {

169

public PyUnicode(String string);

170

public PyUnicode(char[] chars);

171

public PyUnicode(int codepoint);

172

173

// Unicode-specific methods

174

public String encode();

175

public String encode(String encoding);

176

public String encode(String encoding, String errors);

177

public boolean isalnum();

178

public boolean isalpha();

179

public boolean isdecimal();

180

public boolean isdigit();

181

public boolean islower();

182

public boolean isnumeric();

183

public boolean isspace();

184

public boolean istitle();

185

public boolean isupper();

186

}

187

```

188

189

### Numeric Types

190

191

#### PyInteger

192

193

```java { .api }

194

public class PyInteger extends PyObject {

195

public PyInteger(int value);

196

197

// Access methods

198

public int getValue();

199

public int asInt();

200

public long asLong();

201

public double asDouble();

202

203

// Arithmetic operations inherited from PyObject

204

// __add__, __sub__, __mul__, __div__, etc.

205

}

206

```

207

208

#### PyLong

209

210

```java { .api }

211

public class PyLong extends PyObject {

212

public PyLong(long value);

213

public PyLong(BigInteger value);

214

public PyLong(String s);

215

public PyLong(String s, int radix);

216

217

// Access methods

218

public BigInteger getValue();

219

public long asLong();

220

public int asInt();

221

public double asDouble();

222

223

// Bit operations

224

public int bit_length();

225

public PyLong conjugate();

226

}

227

```

228

229

#### PyFloat

230

231

```java { .api }

232

public class PyFloat extends PyObject {

233

public PyFloat(double value);

234

public PyFloat(float value);

235

236

// Access methods

237

public double getValue();

238

public double asDouble();

239

public float asFloat();

240

public int asInt();

241

242

// Float-specific methods

243

public boolean is_integer();

244

public PyTuple as_integer_ratio();

245

public String hex();

246

public static PyFloat fromhex(String s);

247

}

248

```

249

250

#### PyComplex

251

252

```java { .api }

253

public class PyComplex extends PyObject {

254

public PyComplex(double real);

255

public PyComplex(double real, double imag);

256

257

// Access methods

258

public double getReal();

259

public double getImag();

260

public PyFloat real;

261

public PyFloat imag;

262

263

// Complex operations

264

public PyComplex conjugate();

265

public double __abs__();

266

}

267

```

268

269

### Container Types

270

271

#### PyList

272

273

```java { .api }

274

public class PyList extends PySequenceList {

275

public PyList();

276

public PyList(PyObject[] elements);

277

public PyList(Collection<?> c);

278

279

// List operations

280

public void append(PyObject o);

281

public void insert(int index, PyObject o);

282

public PyObject pop();

283

public PyObject pop(int index);

284

public void remove(PyObject o);

285

public void reverse();

286

public void sort();

287

public void sort(PyObject cmp);

288

public void sort(PyObject cmp, PyObject key);

289

public void sort(PyObject cmp, PyObject key, PyObject reverse);

290

public int count(PyObject o);

291

public int index(PyObject o);

292

public int index(PyObject o, int start);

293

public int index(PyObject o, int start, int stop);

294

public void extend(PyObject o);

295

296

// Sequence operations inherited from PySequenceList

297

public int __len__();

298

public PyObject __getitem__(int index);

299

public void __setitem__(int index, PyObject value);

300

public void __delitem__(int index);

301

public PyList __getslice__(int start, int stop, int step);

302

}

303

```

304

305

#### PyTuple

306

307

```java { .api }

308

public class PyTuple extends PySequenceList {

309

public PyTuple();

310

public PyTuple(PyObject[] elements);

311

public PyTuple(Collection<?> c);

312

313

// Tuple is immutable - no modification methods

314

// Sequence operations inherited from PySequenceList

315

public int __len__();

316

public PyObject __getitem__(int index);

317

public PyTuple __getslice__(int start, int stop, int step);

318

public int count(PyObject o);

319

public int index(PyObject o);

320

}

321

```

322

323

#### PyDictionary

324

325

```java { .api }

326

public class PyDictionary extends AbstractDict implements ConcurrentMap {

327

public PyDictionary();

328

public PyDictionary(Map<PyObject, PyObject> map);

329

public PyDictionary(PyObject[] elements);

330

331

// Dictionary operations

332

public PyObject get(PyObject key);

333

public PyObject get(PyObject key, PyObject defaultValue);

334

public void put(PyObject key, PyObject value);

335

public PyObject pop(PyObject key);

336

public PyObject pop(PyObject key, PyObject defaultValue);

337

public PyObject popitem();

338

public void clear();

339

public void update(PyObject other);

340

public void update(PyObject[] args, String[] keywords);

341

342

// View operations

343

public PyList keys();

344

public PyList values();

345

public PyList items();

346

public PyObject iterkeys();

347

public PyObject itervalues();

348

public PyObject iteritems();

349

350

// Dictionary methods

351

public PyObject setdefault(PyObject key);

352

public PyObject setdefault(PyObject key, PyObject defaultValue);

353

public boolean has_key(PyObject key);

354

public PyDictionary copy();

355

}

356

```

357

358

#### PySet and PyFrozenSet

359

360

```java { .api }

361

public class PySet extends BaseSet {

362

public PySet();

363

public PySet(PyObject[] elements);

364

public PySet(PyObject iterable);

365

366

// Mutable set operations

367

public void add(PyObject o);

368

public void remove(PyObject o);

369

public void discard(PyObject o);

370

public PyObject pop();

371

public void clear();

372

public void update(PyObject other);

373

public void intersection_update(PyObject other);

374

public void difference_update(PyObject other);

375

public void symmetric_difference_update(PyObject other);

376

}

377

378

public class PyFrozenSet extends BaseSet {

379

public PyFrozenSet();

380

public PyFrozenSet(PyObject[] elements);

381

public PyFrozenSet(PyObject iterable);

382

383

// Immutable - no modification methods

384

// Set operations return new sets

385

public PyObject union(PyObject other);

386

public PyObject intersection(PyObject other);

387

public PyObject difference(PyObject other);

388

public PyObject symmetric_difference(PyObject other);

389

}

390

```

391

392

## Usage Examples

393

394

### Creating Objects

395

396

```java

397

// Using factory methods

398

PyString str = Py.newString("Hello, World!");

399

PyInteger num = Py.newInteger(42);

400

PyList list = Py.newList();

401

PyDictionary dict = Py.newDict();

402

403

// Using constructors

404

PyString str2 = new PyString("Another string");

405

PyFloat pi = new PyFloat(3.14159);

406

PyTuple tuple = new PyTuple(new PyObject[]{str, num, pi});

407

```

408

409

### Object Manipulation

410

411

```java

412

// Working with lists

413

PyList numbers = Py.newList();

414

numbers.append(Py.newInteger(1));

415

numbers.append(Py.newInteger(2));

416

numbers.append(Py.newInteger(3));

417

418

PyObject first = numbers.__getitem__(Py.newInteger(0));

419

System.out.println(first); // 1

420

421

// Working with dictionaries

422

PyDictionary person = Py.newDict();

423

person.put(Py.newString("name"), Py.newString("Alice"));

424

person.put(Py.newString("age"), Py.newInteger(30));

425

426

PyObject name = person.get(Py.newString("name"));

427

System.out.println(name); // Alice

428

```

429

430

### String Operations

431

432

```java

433

PyString greeting = Py.newString("Hello, World!");

434

PyString upper = greeting.upper();

435

System.out.println(upper); // HELLO, WORLD!

436

437

PyList words = greeting.split(Py.newString(", "));

438

System.out.println(words); // ['Hello', 'World!']

439

440

PyString joined = Py.newString(" | ").join(words);

441

System.out.println(joined); // Hello | World!

442

```

443

444

### Numeric Operations

445

446

```java

447

PyInteger a = Py.newInteger(10);

448

PyInteger b = Py.newInteger(3);

449

450

PyObject sum = a.__add__(b);

451

PyObject diff = a.__sub__(b);

452

PyObject prod = a.__mul__(b);

453

PyObject quot = a.__div__(b);

454

455

System.out.println(sum); // 13

456

System.out.println(diff); // 7

457

System.out.println(prod); // 30

458

System.out.println(quot); // 3

459

```

460

461

### Type Checking and Conversion

462

463

```java

464

PyObject obj = Py.newString("123");

465

466

// Type checking

467

if (obj instanceof PyString) {

468

System.out.println("It's a string");

469

}

470

471

// Convert to Java types

472

String javaStr = obj.toString();

473

int javaInt = Integer.parseInt(javaStr);

474

475

// Convert back to Python

476

PyInteger pyInt = Py.newInteger(javaInt);

477

```

478

479

## Constants and Singletons

480

481

The `Py` class provides common Python constants:

482

483

```java { .api }

484

public final class Py {

485

public static final PyObject None;

486

public static final PyObject Ellipsis;

487

public static final PyObject NotImplemented;

488

public static final String[] NoKeywords;

489

public static final PyObject[] EmptyObjects;

490

public static final PyFrozenSet EmptyFrozenSet;

491

public static final PyTuple EmptyTuple;

492

public static final PyInteger Zero;

493

public static final PyInteger One;

494

public static final PyBoolean False;

495

public static final PyBoolean True;

496

public static final PyString EmptyString;

497

public static final PyUnicode EmptyUnicode;

498

public static final PyString Newline;

499

public static final PyUnicode UnicodeNewline;

500

public static final PyString Space;

501

public static final PyUnicode UnicodeSpace;

502

}

503

```

504

505

### Usage Examples

506

507

```java

508

// Using constants

509

PyObject result = condition ? Py.True : Py.False;

510

PyList emptyList = Py.newList(Py.EmptyObjects);

511

PyTuple emptyTuple = Py.EmptyTuple;

512

513

// None comparisons

514

if (value == Py.None) {

515

System.out.println("Value is None");

516

}

517

```