# Python elide module

> The Python `elide` module: evaluate JavaScript with `elide.js`, share Python functions with `elide.bind` and `@elide.poly(name=…)`, and reach telemetry controls.

Python code running on Elide can `import elide` without installing anything.

| Name | Effect |
| --- | --- |
| `elide.js(source)` | Evaluates a JavaScript string and returns its value. Needs `--interop`. |
| `elide.bind(fn)` | Shares `fn` under its `__name__`; returns `fn`, so it works as a decorator. |
| `elide.poly(name=…)` | Decorator that shares a function under `name`. |
| `elide.telemetry` | Telemetry controls; see [OpenTelemetry](https://elide.help/docs/opentelemetry/python). |

## `elide.js`

```python
# calls.py
from elide import js

slug = js("(s) => s.trim().toLowerCase().replaceAll(' ', '-')")
print(slug("  Hello Polyglot World "))

point = js("({ x: 1, y: 2, tags: ['a', 'b'] })")
print(point.x + point.y, point.tags[0], len(point.tags))
```

```bash
elide --interop python calls.py
```

```text
hello-polyglot-world
3 a 2
```

JavaScript functions are callable, objects expose their properties as attributes, and `undefined` becomes `None`.
[Build a polyglot app](https://elide.help/docs/polyglot) lists every value conversion.

| Input | Result |
| --- | --- |
| Run without `--interop` | `ValueError: polyglot language 'js' not found` |
| JavaScript that throws or fails to parse | `RuntimeError: JSException: SyntaxError: <elide.js>:1:3 Expected an operand but found eof` |
| A non-string argument | `TypeError: elide.js() argument source must be str, not int` |

## `elide.bind` and `elide.poly`

Both share a Python function by name. Python reads shared values with `polyglot.import_value`, and the
[REPL](https://elide.help/docs/repl) with `/import NAME`.

```python
# shared.py
import polyglot
from elide import bind, poly

@bind
def add(a, b):
    return a + b

@poly(name="greet")
def say_hello(name):
    return f"hello {name}"

print(add(2, 3), say_hello("elide"))
print(polyglot.import_value("add")(2, 3))
print(polyglot.import_value("greet")("elide"))
```

```bash
elide python shared.py
```

```text
5 hello elide
5
hello elide
```

- Both decorators return the original function, so it stays callable from Python.
- `elide.poly(name=1)` raises `TypeError: elide.poly() argument name must be str, not int`.
- JavaScript files have no `Polyglot` global, so they cannot read shared values. To call Python from JavaScript,
  import the `.py` module instead; see [Languages and interop](https://elide.help/docs/languages).

## See also

- [Build a polyglot app](https://elide.help/docs/polyglot)
- [Languages and interop](https://elide.help/docs/languages)
- [Python toolchain](https://elide.help/docs/python-toolchain)
