Statements¶
Function Definitions¶
LisPy version
(deco [decorator1
decorator2]
(def function-name [arg1 arg2 *args :kwarg1 value1 :kwarg2 value2 **kwargs]
"docstring"
body1
body2
(return value)))
@decorator1
@decorator2
def function_name(arg1, arg2, *args, kwarg1=value1, kwarg2=value2, **kwargs):
"""docstring"""
body1
body2
return value
Async Function Definitions¶
Use async-def instead of def.
deco¶
Decorates the definition (def, async-def or class) it wraps: (deco decorator definition) for one decorator, (deco [d1 d2] definition) for several, listed top to bottom and applied bottom-up as in Python: the last one wraps the definition first, so (deco [d1 d2] ...) is d1(d2(f)). A decorator is any expression, a call included.
LisPy version
(deco my-decorator
(def f [x] (return x)))
(deco [d1 (d2 1)]
(def g [x] (return x)))
(deco register
(class A []
(pass)))
return¶
(return) returns None; (return value) returns the value. It takes at most one value: return several as a tuple with (, a b).
LisPy version
Python versionClass Definition¶
LisPy version
Python versionGeneric Functions and Classes¶
Python 3.12+ (PEP 695); defaults need
Python 3.13+ (PEP 696). Write the type
parameters glued to the name, with no space: f[T] is a generic name, while
f [T] is a name followed by a list. The bracket uses the argument markers of
def: ^ for a bound (a tuple bound is a constraint list), * for a
TypeVarTuple, ** for a ParamSpec and : for a default. On an older Python
these forms are a compile error naming the version they need.
A glued name is only valid as the name of def, async-def, class and
type; to subscript, use (sub xs ...).
LisPy version
(def first[T] [xs ^(sub list T)] ^T
(return (sub xs 0)))
(class Box[T ^(, int str) *Ts **P] [Base]
(pass))
(def f[:T str :U ^int bool :*Ts *(sub tuple int) :**P [int str]] [] ; Python 3.13+
(pass))
def first[T](xs: list[T]) -> T:
return xs[0]
class Box[T: (int, str), *Ts, **P](Base):
pass
def f[T = str, U: int = bool, *Ts = *tuple[int], **P = [int, str]](): # Python 3.13+
pass
Type Alias¶
Python 3.12+ (PEP 695). As in Python,
type is a soft keyword: (type NAME value) and (type NAME[T ...] value)
are a type alias only as a statement with exactly two operands whose first is a
plain name. Every other shape is a call to the builtin type, so
(= t (type x)) and (type "N" (, object) {}) are unchanged. If you rebind
type and need a bare two-argument call as a statement, write
(= _ (type a b)). On an older Python the alias is a compile error naming the
version it needs.
LisPy version
Python versiondel¶
LisPy version
Python versionAssignment¶
LisPy version
Python versionAugmented Assignment¶
LisPy version
Python versionType Annotations¶
Simple Type Annotations¶
LisPy version
Python versionAssignment with Type Annotations¶
LisPy version
Python versionFunction Definition with Type Annotations¶
LisPy version
Python versionPydantic Example¶
LisPy version
(class User [BaseModel]
(= id ^int)
(= name ^str "John Doe")
(= signup_ts ^(| datetime None))
(= tastes ^(sub dict (, str PositiveInt))))
class User(BaseModel):
id: int
name: str = "John Doe"
signup_ts: datetime | None
tastes: dict[str, PositiveInt]
for¶
LisPy version
Python versionAsynchronous for¶
Use async-for instead of for.
while¶
LisPy version
Python versionif¶
LisPy version
Python versiondo block¶
If you want to use multiple statements in then-body or else-body, you can use do block.
LisPy version
with¶
Simple with¶
LisPy version
Python versionwith with alias¶
LisPy version
Python versionAsynchronous with¶
Use async-with instead of with.
match¶
LisPy version
(match x
(case "Relevant"
...)
(case None
...)
(case [1 2]
...)
(case [1 2 *rest]
...)
(case [*_]
...)
(case {1 _ 2 _}
...)
(case {**rest}
...)
(case (Point2D 0 0)
...)
(case (Point3D :x 0 :y 0 :z 0)
...)
(case [x] as y
...)
(case _
...)
(case (| [x] y)
...))
match x:
case "Relevant":
...
case None:
...
case [1, 2]:
...
case [1, 2, *rest]:
...
case [*_]:
...
case {1: _, 2: _}:
...
case {**rest}:
...
case Point2D(0, 0):
...
case Point3D(x=0, y=0, z=0):
...
case [x] as y:
...
case _:
...
case [x] | y:
...
Signed Literal Patterns¶
A negative number or a complex literal is a value pattern. A leading + needs
Python 3.15+; on an older Python it is a compile error naming the version.
LisPy version
Python versionraise¶
LisPy version
Python versiontry¶
LisPy version
(try
body1
body2
(except [Exception1]
body3
body4)
(except [Exception2 as e]
body5
body6)
(else
body7
body8)
(finally
body9
body10))
try:
body1
body2
except Exception1:
body3
body4
except Exception2 as e:
body5
body6
else:
body7
body8
finally:
body9
body10
except* clause¶
LisPy version
Python versionassert¶
LisPy version
Python versionimport¶
Simple import¶
LisPy version
Python versionfrom import¶
without alias¶
LisPy version
Python versionwith alias¶
LisPy version
Python versionLazy imports¶
Python 3.15+ (PEP 810). lazy-import and
lazy-from take the same arguments as import and from; the module is
loaded when a name is first used. As in Python, lazy-from refuses * and
__future__, and a lazy import is not allowed inside a function, a class or a
try block. On an older Python these forms are a compile error naming the
version they need.
LisPy version
Python versionglobal¶
LisPy version
Python versionnonlocal¶
LisPy version
Python versionpass¶
LisPy version
Python versionbreak¶
LisPy version
Python versioncontinue¶
LisPy version
Python version