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.

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

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:
        ...

raise

LisPy version

(raise Exception)
(raise Exception from ValueError)
Python version
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

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