Skip to content

Cheat Sheet: Python

gilch edited this page Dec 16, 2024 · 11 revisions

This is a cheat sheet, not a tutorial. It's meant to refresh your memory or help you find search terms for the docs, for those familiar with Python. Not all alternatives are macros. Not all macros are listed, only those that correspond to Python language features.

Python Statement Alternatives
(expression statements) progn, many macros with a *body arg
= (simple assignments) let (locals), define
foo = bar = (chained assigns) set! (items), set@ (attributes)
foo, bar = (unpacking assign) let-from, let*from, destruct->, my#
+=, etc. (augmented assigns) zap! (items), zap@ (attributes)
foo[bar] = (item assigns) set!, setitem
foo.bar = (attr assigns) set@, setattr, attach
@ (decorators) :@## on a def- macro, or zap@ afterward
(annotations) Mutate __annotations__ as needed.
pass Not required. None, (), empty body.
del del (at top level), delitem, delattr
return Not required. Rewrite to tail position.
raise throw
from (of raise) throw-from
break (in for) truthy tail in any-map or any*map (rewrite to tail position)
break (in while) predicate in takewhile, don't continue in recur-from or Ensue (prelude)
continue Not required. Rewrite to tail position.
import foo .#"import foo" (at top level), foo. (module handle)
import foobar as fb .#"import foobar as fb" (at top level), (define fb foobar.), (alias fb foobar.)
global define
if when
else (of if) if-else
elif cond
else (of for) any-map/any*map as unless condition
else (of while) loop-from result as when or unless condition
try engarde (prelude)
finally context manager decorators. engarde (prelude)
except Ensue X attribute, engarde (prelude)
else (of try) engarde (prelude) result as when or unless condition
def defun
class type, types..new_class, deftypeonce (with defun/define, etc.)
assert assure (also consider avow)
yield Ensue (prelude) Y attribute
yield from Ensue (prelude) Y & F attributes.
with ContextDecorator, enter (prelude)
nonlocal Not applicable (locals are single assignment in standard Hissp). Use my# attributes instead.
async/await types.coroutine with Ensue (prelude)
match destruct->, sort of. Combine with cond, etc. or polymorphism (e.g. @singledispatch).
Python Expression Alternatives
(operators) operator. (module handle), op# (alias), prelude (star imports). let, mix, XY#|X*Y|, |X*Y|.
(literals) Mostly the same.
foo[bar] (subscription lookups) getitem, slice, !##, [##
b"bytes" |b"bytes"|, b#"bytes", B#|bytes|
foo(a, b) (calls) (foo a b)
foo.bar(a, b) (method calls) (.bar foo a b)
-.foo (attribute identifiers) Mostly the same if root is identifier. getattr, itemgetter, @##', let, X#X.foo
lambda lambda (special form)
(1, 2, 3) (simple tuples) '(1 2 3)
('a', 'b') (string tuples) '(a b) (identifiers only), '(|a| |b|)
(x, y) (interpolated tuples) `(,x ,y), (en#tuple x y), (|| x y ||)
{'k': v} (dicts) (dict : k v), (% 'k v), (dict `((,k ,v)))
[a, b, c] (lists) (en#list a b c), (@ a b c), (list `(,a ,b ,c))
{a, b, c} (sets) (en#set a b c), (# a b c), (set `(,a ,b ,c))
(*foo, 2) (tuple unpacking) `(,@foo 2), (en#tuple : :* foo :? 2), (|| : :* foo :? 2 :? ||)
[*foo, 2] (list unpacking) (list `(,@foo 2)), (@ :* foo 2)
{*foo, 2} (set unpacking) (set `(,@foo 2)), (# :* foo 2)
{**foo, 'a': 2} (dict unpacking) (dict foo : a 2) (identifier key), (dict `(,@(.items foo) (a 2))), (% :** foo 'a 2)
(-for-in-if-) (genexpr/comprehensions) Builtins and itertools: map, filter, filterfalse, zip, starmap, filter, chain#, X#. |-|/mix if you're desperate. recur-from or Ensue (prelude) if it's too complicated.
yield Ensue's .Y (prelude)
f"string{s}" .format, |f"string{s}"| (but expressions are in Python), (define enjoin en#X#(.join "" (map str X))) ((enjoin "string"s)), (mix |f"string{|s|"|).
not not (when called directly) or operator..not_ (for higher-order functions)
and/or ands/ors
:= (walrus/assignment expression) my#, let

Remember, almost any Python works in |-|, even one-line statements at the top level. Double the | to escape. .# can also work on a computed Python string. There's always exec if you really need it, and there are appropriate (and inappropriate) uses.

Clone this wiki locally