Skip to content

Statements

Function Definitions

LisPy version

(deco [decorator1
       decorator2]
  (def function-name [arg1 arg2 *args :kwarg1 value1 :kwarg2 value2 **kwargs]
    "docstring"
    body1
    body2
    (return value)))
Python version
@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)))
Python version
@my_decorator
def f(x):
    return x


@d1
@d2(1)
def g(x):
    return x


@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

(def f [x]
  (return x))
(def g []
  (return))
(def h [x y]
  (return (, x y)))
Python version
def f(x):
    return x


def g():
    return


def h(x, y):
    return (x, y)

Class Definition

LisPy version

(deco [decorator1
       decorator2]
  (class class-name [base-class1 base-class2]
    "docstring"
    body))
Python version
@decorator1
@decorator2
class class_name(base_class1, base_class2):
    """docstring"""

    body

Generic 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))
Python version
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

(type Number (| int float))
(type Vec[T] (sub list T))
Python version
type Number = int | float
type Vec[T] = list[T]

del

LisPy version

(del x y z ...)
Python version
del x, y, z, ...

Assignment

LisPy version

(= x value)
(= [x y *args] [value1 value2 value3 value4, ...]) ; destructuring assignment
Python version
x = value
[x, y, *args] = [value1, value2, value3, value4, ...]
# then args = [value3, value4, ...]

Augmented Assignment

LisPy version

(+= x value)
(-= x value)
(*= x value)
(/= x value)
...
Python version
x += value
x -= value
x *= value
x /= value
...

Type Annotations

Simple Type Annotations

LisPy version

(= x ^type)
Python version
x: type

Assignment with Type Annotations

LisPy version

(= a ^int 1)
Python version
a: int = 1

Function Definition with Type Annotations

LisPy version

(def a [b ^int c] ^float
  (return (+ b c)))
Python version
def a(b: int, c) -> float:
    return b + c

Pydantic Example

LisPy version

(class User [BaseModel]
  (= id ^int)
  (= name ^str "John Doe")
  (= signup_ts ^(| datetime None))
  (= tastes ^(sub dict (, str PositiveInt))))
Python version
class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: datetime | None
    tastes: dict[str, PositiveInt]

for

LisPy version

(for x in xs
  body1
  body2)
Python version
for x in xs:
    body1
    body2

Asynchronous for

Use async-for instead of for.

while

LisPy version

(while condition
  body1
  body2)
Python version
while condition:
    body1
    body2

if

LisPy version

(if test
    then
    else)
Python version
if test:
    then
else:
    else

do block

If you want to use multiple statements in then-body or else-body, you can use do block. LisPy version

(if test
    (do then1 then2)
    (do else1 else2))
Python version
if test:
    then1
    then2
else:
    else1
    else2

with

Simple with

LisPy version

(with [expression]
  body)
Python version
with expression:
    body

with with alias

LisPy version

(with [expression as target]
  body)
Python version
with expression as target:
    body

Asynchronous 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)
    ...))
Python 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:
        ...

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

(match x
  (case -1
    ...)
  (case -1-2j
    ...)
  (case {-1 _}
    ...)
  (case +1  ; Python 3.15+
    ...))
Python version
match x:
    case -1:
        ...
    case -1 - 2j:
        ...
    case {-1: _}:
        ...
    case +1:  # Python 3.15+
        ...

raise

LisPy version

(raise)
(raise Exception)
(raise Exception from ValueError)
Python version
raise
raise Exception
raise Exception from ValueError

try

LisPy version

(try
  body1
  body2
  (except [Exception1]
    body3
    body4)
  (except [Exception2 as e]
    body5
    body6)
  (else
    body7
    body8)
  (finally
    body9
    body10))
Python version
try:
    body1
    body2
except Exception1:
    body3
    body4
except Exception2 as e:
    body5
    body6
else:
    body7
    body8
finally:
    body9
    body10

except* clause

LisPy version

(try
  (raise BlockingIOError)
  (except* [BlockingIOError as e]
    (print (repr e))))
Python version
try:
    raise BlockingIOError
except* BlockingIOError as e:
    print(repr(e))

assert

LisPy version

(assert condition)
(assert condition "message")
Python version
assert condition
assert condition, "message"

import

Simple import

LisPy version

(import module)
(import module as alias)
Python version
import module
import module as alias

from import

without alias

LisPy version

(from module [name1 name2 name3])
Python version
from module import name1, name2, name3

with alias

LisPy version

(from module [name1
              name2 as alias
              name3])
Python version
from module import name1
from module import name2 as alias
from module import name3

Lazy 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

(lazy-import json)
(lazy-import numpy as np)
(lazy-from json [dumps loads as load-json])
Python version
lazy import json
lazy import numpy as np
lazy from json import dumps, loads as load_json

global

LisPy version

(global x y z ...)
Python version
global x, y, z, ...

nonlocal

LisPy version

(nonlocal x y z ...)
Python version
nonlocal x, y, z, ...

pass

LisPy version

(pass)
Python version
pass

break

LisPy version

(break)
Python version
break

continue

LisPy version

(continue)
Python version
continue