Skip to content

Commit fdbc66d

Browse files
committed
Improve @py macro documentation
1 parent 8eed6ee commit fdbc66d

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

src/PyMacro/PyMacro.jl

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -860,18 +860,63 @@ end
860860
"""
861861
@py expr
862862
863-
Evaluate the given expression using Pythonic semantics.
864-
865-
For example:
866-
- `f(x, y)` is translated to `pycall(f, x, y)`
867-
- `x + y` is translated to `pyadd(x, y)`
868-
- `x === y` is translated to `pyis(x, y)` (`x is y` in Python)
869-
- `x.foo` is translated to `pygetattr(x, "foo")`
870-
- `import x: f as g` is translated to `g = pyimport("x" => "f")` (`from x import f as g` in Python)
871-
872-
Compound statements such as `begin`, `if`, `while` and `for` are supported.
873-
874-
See the online documentation for more details.
863+
Evaluate `expr` using Python syntax and semantics while staying in Julia.
864+
The macro lowers the provided Julia syntax tree to calls into PythonCall's
865+
runtime (for example, `pycall`, `pyadd`, `pygetattr`, and friends) and returns
866+
the resulting `Py` object.
867+
868+
Supported syntax includes:
869+
870+
* **Literals and names** – Numeric and string literals, `None`, `True`, `False`,
871+
container displays (`(…)`, `[…]`, `{…}`, `{key:value, …}`) and special
872+
placeholders such as `__file__` and `__line__`.
873+
* **Calls and operators** – Function calls are translated to `pycall`, unary
874+
and binary operators are forwarded to the corresponding `py*` helper
875+
(e.g. `x + y` → `pyadd(x, y)`, `x === y` → `pyis(x, y)`), and chained
876+
arithmetic or comparison expressions behave like their Python equivalents.
877+
* **Attribute access and indexing** – `obj.attr`, `obj[key]`, and slice syntax
878+
(`obj[start:stop:step]`) map to `pygetattr`, `pygetitem`, and `pyslice`.
879+
* **Statements inside blocks** – Within `@py begin … end` you can perform
880+
assignments, `@del` deletions, `if`/`elif`/`else`, `while`, and `for`
881+
statements, and short-circuit boolean logic (`&&`/`||`). Import statements
882+
(`import pkg`, `import pkg as alias`, `from pkg import name as alias`) are
883+
also supported.
884+
* **Interop helpers** – Use `@jl expr` to splice the result of a Julia
885+
expression into the Python evaluation. The auxiliary macros `@compile`,
886+
`@eval`, and `@exec` wrap `pybuiltins.compile` to provide the same behaviour
887+
as their Python counterparts.
888+
889+
Names that match Python builtins are resolved through `pybuiltins`; other
890+
identifiers are captured from the surrounding Julia scope. Convert the result
891+
to Julia values with `pyconvert` or related helpers if desired.
892+
893+
# Examples
894+
895+
```julia
896+
julia> @py begin
897+
import math: sqrt
898+
sqrt(9)
899+
end
900+
Py(3.0)
901+
902+
julia> factor = 10
903+
julia> @py begin
904+
data = [1, 2, @jl factor]
905+
total = 0
906+
for value in data
907+
total = total + value
908+
end
909+
total
910+
end
911+
Py(13)
912+
913+
julia> @py begin
914+
info = {"x": 1, "y": 2}
915+
@del info["x"]
916+
info
917+
end
918+
Py({'y': 2})
919+
```
875920
876921
!!! warning
877922
This macro is experimental. It may be modified or removed in a future release.

0 commit comments

Comments
 (0)