Form Reference¶
Every LisPython form with its usage and a one-line description: the same text the editor shows on hover. Each form links to its examples on the Statements, Expressions and Macros pages.
Statements¶
| Form | Usage | Description |
|---|---|---|
def |
(def name [args] body...) |
Define a function. Arguments are given in a bracket list. An optional docstring can follow the argument list. |
async-def |
(async-def name [args] body...) |
Define an async function (coroutine). |
deco |
(deco decorator definition) or (deco [decorator ...] definition) |
Apply a decorator to the following function/class definition. |
return |
(return value) |
Return a value from a function. |
class |
(class name [bases] body...) |
Define a class. Bases are given in a bracket list. |
type |
(type Name value)(type Name[T] value) |
Define a type alias (Python 3.12+). |
del |
(del target...) |
Delete a name or item. |
= |
(= target... value) or (= target ^annotation value), value optional after an annotation |
Assignment statement. |
+= |
(+= target value...) |
Augmented assignment (target += value). |
-= |
(-= target value...) |
Augmented assignment (target -= value). |
*= |
(*= target value...) |
Augmented assignment (target *= value). |
/= |
(/= target value...) |
Augmented assignment (target /= value). |
//= |
(//= target value...) |
Augmented assignment (target //= value). |
%= |
(%= target value...) |
Augmented assignment (target %= value). |
**= |
(**= target value...) |
Augmented assignment (target **= value). |
<<= |
(<<= target value...) |
Augmented assignment (target <<= value). |
>>= |
(>>= target value...) |
Augmented assignment (target >>= value). |
|= |
(|= target value...) |
Augmented assignment (target |= value). |
^= |
(^= target value...) |
Augmented assignment (target ^= value). |
&= |
(&= target value...) |
Augmented assignment (target &= value). |
@= |
(@= target value...) |
Augmented assignment (target @= value). |
for |
(for target in iterable body...) |
For loop. target is bound to each element of iterable. |
async-for |
(async-for target in iterable body...) |
Async for loop — use inside async-def. |
while |
(while test body...) |
While loop. |
if |
(if test then else...) |
Conditional statement. Compiles to Python if/else. |
do |
(do body...) |
A statement block grouping several statements where one statement is expected (e.g. an if branch); not an expression. |
with |
(with [context as target ...] body...), each as target optional |
Context manager statement. |
async-with |
(async-with [context as target ...] body...), each as target optional |
Async context manager — use inside async-def. |
case |
(case pattern as name if guard body...), as name and if guard optional |
One arm of a match: a pattern, an optional as name and if guard, then the body. |
raise |
(raise), (raise exception) or (raise exception from cause) |
Raise an exception. |
try |
(try body... (except ...)... (else ...) (finally ...)) |
Try/except/finally statement. |
except |
(except [type ... as name] body...) or (except body...), as name optional |
Exception handler clause of a try. |
else |
(else body...) |
Clause of a try that runs when no exception was raised. |
finally |
(finally body...) |
Clause of a try that always runs on exit. |
except* |
(except* [type ... as name] body...), as name optional |
Exception-group handler clause of a try (Python 3.11+). |
assert |
(assert test) or (assert test message) |
Assert that a condition is true. |
import |
(import module)(import module as alias) |
Import a module. |
from |
(from module [names...])(from module [name as alias ...]) |
Import specific names from a module. |
lazy-import |
(lazy-import module)(lazy-import module as alias) |
Import a module lazily: it is loaded on first use (Python 3.15+). |
lazy-from |
(lazy-from module [names...])(lazy-from module [name as alias ...]) |
Import names from a module lazily: it is loaded on first use (Python 3.15+). |
global |
(global name...) |
Declare names as global variables. |
nonlocal |
(nonlocal name...) |
Declare names as nonlocal variables. |
pass |
(pass) |
No-op placeholder statement. |
break |
(break) |
Break out of a loop. |
continue |
(continue) |
Continue to the next loop iteration. |
Expressions¶
| Form | Usage | Description |
|---|---|---|
match |
(match subject (case ...)...) |
Structural pattern matching (Python 3.10+). |
and |
(and value...) |
Logical AND (short-circuit). |
or |
(or value...) |
Logical OR (short-circuit). |
| |
(| a b ...)(| pattern pattern...) |
Bitwise OR as an operator; inside a case, an or-pattern matching any alternative. |
+ |
Arithmetic addition / unary positive. | |
- |
Arithmetic subtraction / unary negative. | |
* |
Arithmetic multiplication / iterable unpacking. | |
/ |
True division. | |
// |
Floor (integer) division. | |
% |
Modulo (remainder). | |
** |
Exponentiation. | |
@ |
Matrix multiplication. | |
<< |
Bitwise left shift. | |
>> |
Bitwise right shift. | |
^ |
Bitwise XOR. | |
& |
Bitwise AND. | |
not |
Logical NOT. | |
~ |
Bitwise NOT / unquote (inside quasiquote). | |
== |
(== a b...) |
Equality test. |
> |
(> a b...) |
Greater than. |
!= |
(!= a b...) |
Inequality test. |
< |
(< a b...) |
Less than. |
<= |
(<= a b...) |
Less than or equal. |
>= |
(>= a b...) |
Greater than or equal. |
is |
(is a b...) |
Identity test (x is y). |
is-not |
(is-not a b...) |
Negated identity test (x is not y). |
in |
(in a b...) |
Membership test (x in y). |
not-in |
(not-in a b...) |
Negated membership test (x not in y). |
:= |
(:= name value) |
Walrus operator (assignment expression). |
lambda |
(lambda [args] body) |
Anonymous function (lambda expression). |
ife |
(ife test then else), else optional |
Ternary conditional expression (then if test else else). |
, |
(, a b c) |
Tuple literal. |
await |
(await value) |
Await a coroutine. |
yield |
(yield) or (yield value) |
Yield a value from a generator. |
yield-from |
(yield-from iterable) |
Yield all values from a sub-generator. |
f-string |
f"text {expr} more" |
Formatted string literal (f-string). |
. |
(. value attr...) |
Attribute access (obj.attr); (. obj a b) is obj.a.b. |
sub |
(sub value index...) |
Subscript access (obj[index]); more indexes subscript again. |
Macro system¶
| Form | Usage | Description |
|---|---|---|
' |
Quote — prevent evaluation; return the form as data. | |
` |
Quasiquote — like quote but allows unquote (~) splicing. |
|
~@ |
Unquote-splice — splice a list into a quasiquote. | |
defmacro |
(defmacro name [args] body...) |
Define a compile-time macro. The macro receives unevaluated S-expressions and must return an S-expression. |
require |
(require module)(require module *)(require module [names...]) |
Import macros from a LisPython macro module at compile time. (require module) registers every macro as module.name, * registers every macro unqualified, and a list registers exactly those names — an empty [] registers none. |
Built-in macros¶
The built-in macros (cond, ->, fn, ...) load in every module with no require: .lpy files, lpy -c, the REPL, nREPL, l2py and the LSP. Making (require lispy.macros [-> cond]) a module's first form (after an optional docstring) keeps only those, and (require lispy.macros []) none. Binding a name that is an active macro is a compile error.
| Form | Usage | Description |
|---|---|---|
cond |
(cond test then test then ... else) |
Nested if statements: each test/then pair is one branch (elif after the first) and a trailing odd operand is the final else. It is a statement and yields no value: use conde for a value, and (do ...) for a branch of several forms. |
conde |
(conde test then test then ... else) |
The expression form of cond: a chain of ife whose trailing odd operand is the final else. An expression needs a value on every path, so an even number of operands (no else) is a compile error, like Python's x if c. |
-> |
(-> value form ...) |
Thread-first: insert value as the first argument of the first form, that result as the first argument of the next, and so on. A form that is not a parenthesised call, such as a bare name, is called with the value: (-> x f (g 2)) is g(f(x), 2). |
->> |
(->> value form ...) |
Thread-last: insert value as the last argument of the first form, that result as the last argument of the next, and so on. A form that is not a parenthesised call, such as a bare name, is called with the value: (->> x f (g 1)) is g(1, f(x)). |
as-> |
(as-> value name form ...) |
Bind name to value, then to each form's result in turn, and yield the last result, so name can stand anywhere in a form. An expression: each form is evaluated once, left to right, in the caller's scope. |
fn |
(fn [args] body) |
Alias of lambda: an anonymous function whose body is one expression. |
comment |
(comment body...) |
Ignore the body: nothing in it is compiled or evaluated, and the form expands to None. |
when |
(when test body...) |
An if statement with no else, whose branch runs every body form in order. A statement: it yields no value. The body must hold a statement, as Python's if does: (when test) is a compile error. |
inc |
(inc value) |
The expression value + 1; value itself is left unchanged. |
dec |
(dec value) |
The expression value - 1; value itself is left unchanged. |
++ |
(++ target) |
The statement target += 1. |
-- |
(-- target) |
The statement target -= 1. |