Skip to content

Commit 625f786

Browse files
authored
Improve @py macro documentation (#712)
* Improve @py macro documentation * docstring edits
1 parent 8eed6ee commit 625f786

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

src/PyMacro/PyMacro.jl

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -860,18 +860,59 @@ 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-like syntax and semantics and return the
864+
resulting [`Py`](@ref) object.
865+
866+
Supported syntax includes:
867+
868+
* **Literals and names:** Numeric and string literals, `None`, `True`, `False`,
869+
containers (`(…)`, `[…]`, `{…}`, `{key:value, …}`) and special
870+
placeholders such as `__file__` and `__line__`.
871+
* **Calls and operators:** Function calls are translated to `pycall`, unary
872+
and binary operators are forwarded to the corresponding `py*` helper
873+
(e.g. `x + y` → `pyadd(x, y)`, `x === y` → `pyis(x, y)`), and chained
874+
arithmetic or comparison expressions behave like their Python equivalents.
875+
* **Attribute access and indexing:** `obj.attr`, `obj[key]`, and slice syntax
876+
(`obj[start:stop:step]`) map to `pygetattr`, `pygetitem`, and `pyslice`.
877+
* **Statements:** Assignments, `@del` deletions, `if`/`elseif`/`else`, `while`, `for`,
878+
short-circuit boolean logic (`&&`/`||`). Import statements
879+
(`import pkg`, `import pkg as alias`, `import pkg: attr as alias`) are
880+
also supported.
881+
* **Interop helpers:** Use `@jl expr` to splice the result of a Julia
882+
expression into the Python evaluation. The auxiliary macros `@compile`,
883+
`@eval`, and `@exec` work like `compile`/`eval`/`exec` in Python but compile
884+
the code argument once and reuse it for speed.
885+
886+
Names that match Python builtins are resolved through [`pybuiltins`](@ref); other
887+
identifiers are captured from the surrounding Julia scope.
888+
889+
# Examples
890+
891+
```julia
892+
julia> @py begin
893+
import math: sqrt
894+
sqrt(9)
895+
end
896+
Py(3.0)
897+
898+
julia> factor = 10
899+
julia> @py begin
900+
data = [1, 2, @jl factor]
901+
total = 0
902+
for value in data
903+
total = total + value
904+
end
905+
total
906+
end
907+
Py(13)
908+
909+
julia> @py begin
910+
info = {"x": 1, "y": 2}
911+
@del info["x"]
912+
info
913+
end
914+
Py({'y': 2})
915+
```
875916
876917
!!! warning
877918
This macro is experimental. It may be modified or removed in a future release.

0 commit comments

Comments
 (0)