Built-in functions
Edge Python provides 68 built-in functions. They are first-class values, so you can pass them as arguments, store them in containers, and alias them.
[3, '-0x3', '-3'] aliased
There is no eval, exec, compile, open, or __import__. Static imports and the sandbox rule them out.
Output
print(*args, sep=' ', end='\n') writes the arguments joined by sep, then end. * unpacking spreads an iterable into the arguments. The file and flush keywords are accepted and ignored.
1 2 3 a-b-c no newline! 1, 2, 3
input
input() pops one line from the host-provided input buffer and returns it as a string. There is no prompt argument. The CLI fills the buffer from piped stdin, one line per call. An empty buffer raises RuntimeError. In WASM the host copies stdin bytes into the guest input buffer before running.
Numeric
abs
abs(x) returns the absolute value of an int or float. Other types raise TypeError.
7 3.14
round
round(x) rounds to the nearest integer and returns an int. Ties go to even. round(x, n) rounds to n decimal digits and returns a float. A negative n rounds to tens, hundreds, and so on.
2 0 -2 1.6 1200
min, max
min(a, b, ...) takes several values or a single iterable. max works the same way. An empty iterable raises ValueError unless a default= is given. A key= function selects the comparison value while the original element is returned.
1 4 -1 bb
sum
sum(iterable) or sum(iterable, start). An empty iterable sums to start, which defaults to 0.
6 106 30
pow
pow(base, exp) matches the ** operator. pow(base, exp, mod) does modular exponentiation on integers. The three-argument form requires a non-negative exponent and a modulus with absolute value at most 2^63. A zero modulus raises ZeroDivisionError. The other violations raise ValueError.
1024 24 7
divmod
divmod(a, b) returns (a // b, a % b) as a tuple. Ints and floats both work. Float operands give a float quotient and remainder.
(2, 1) (-3, 2) (3.0, 1.5)
bin, oct, hex
bin(x), oct(x), and hex(x) format an integer in base 2, 8, or 16 with the matching prefix.
0b1010 0o10 0xff -0x100
Type conversion
int
int(x) accepts an int, bool, float, or numeric string. Floats truncate toward zero. Strings accept _ as a digit separator. int(s, base) parses a string in radix 2 to 36, or radix 0 to auto-detect a 0x, 0o, or 0b prefix. Bad strings raise ValueError. int(inf) raises OverflowError and int(nan) raises ValueError. Results are bounded by the integer width.
3 42 1 255 31 1000
float
float(x) accepts an int, bool, float, or string. Strings recognize inf, -inf, and nan, case-insensitively.
2.0 3.14 inf
str
str(x) returns the display form of x. No argument gives an empty string. str(bytes, encoding) decodes bytes like bytes.decode.
42 [1, 2, 3] None hi
bool
bool(x) returns the truth value of x. The rules live in Truthy and falsy.
False True False True False True
list, tuple, set, frozenset
Each accepts any iterable and builds a new container. Iterating a dict yields its keys. With no argument, each builds an empty container. A live generator object (a def with yield) is only accepted by list(). The others raise TypeError.
['a', 'b', 'c']
(0, 1, 2)
{'b', 'a'}
frozenset({1, 2, 3})dict
dict() builds from a mapping, an iterable of key/value pairs, keyword arguments, or a mix. Each pair must have length 2.
{'a': 1, 'b': 2}
{'a': 1}
{'a': 1, 'b': 2}chr, ord
chr(i) returns the one-character string for code point i, across full Unicode. Out-of-range values raise ValueError. ord(c) is the inverse and accepts a length-1 string or length-1 bytes.
A 65 65 😀
Sequences and iteration
len
len(x) returns the element count of a string (in code points), bytes, list, tuple, dict, set, frozenset, or range. Other types raise TypeError.
5 4 2 100
range
range(stop), range(start, stop), or range(start, stop, step). Lazy. A zero step raises ValueError and non-integer arguments raise TypeError. Two ranges compare equal when they produce the same sequence of values.
[0, 1, 2, 3, 4] [2, 3, 4, 5, 6, 7] [10, 8, 6, 4, 2] True
sorted
sorted(iterable) returns a new sorted list. key=fn compares by fn(item). reverse=True flips the order. Numbers, strings, bytes, and lists or tuples order lexicographically. Objects with __lt__ sort by it. Mixing unordered types raises TypeError.
[1, 1, 3, 4, 5] ['e', 'h', 'l', 'l', 'o'] [5, 4, 3, 1, 1] ['kiwi', 'apple', 'banana']
reversed
reversed(x) returns a new list in reverse order. It is eager, not a lazy iterator. A string becomes a list of one-character strings.
[3, 2, 1] ['c', 'b', 'a']
enumerate
enumerate(iterable) returns a list of (index, value) tuples. A second argument, positional or start=, sets the first index.
0 a 1 b 2 c [(7, 'a'), (8, 'b')]
zip
zip(a, b, ...) returns a list of tuples pairing the inputs, truncated to the shortest. There is no strict= mode.
1 x 2 y [(1, 3, 5), (2, 4, 6)]
iter, next
iter(x) returns a fresh iterator over any iterable. It materialises a snapshot, so the original is never mutated. next(it) returns the next item and raises StopIteration when exhausted. next(it, default) returns default instead of raising. The two-argument iter(callable, sentinel) calls callable() until it returns sentinel.
10 20 30 done
map, filter
map(fn, *iterables) returns a list of fn(items...). Several iterables are walked in parallel and stop at the shortest. filter(pred, iterable) returns a list of items where pred(item) is truthy. A None predicate keeps truthy items. Both are eager.
[2, 4, 6] [11, 22] [3, 4] [1, 'hi', [1]]
all, any
all(x) and any(x) test truthiness across an iterable and short-circuit at the deciding element. all([]) is True and any([]) is False.
True False True True False
slice
slice(stop), slice(start, stop), or slice(start, stop, step) builds a reusable slice object usable as a sequence index.
[20, 30, 40] [10, 30, 50]
Bytes helpers
bytes_fromhex(s) parses a hex string into bytes. ASCII whitespace is ignored and non-hex input raises ValueError.
int_from_bytes(b, order) reads bytes as an unsigned integer. order is "big" or "little". At most 8 bytes, anything longer raises OverflowError.
int_to_bytes(n, length, order) converts a non-negative int to length bytes. length is at most 8. A negative n raises ValueError and a value that does not fit raises OverflowError.
The methods bytes.fromhex, int.from_bytes, and int.to_bytes do the same jobs with default arguments and no 8-byte cap.
b'Hello' 256 b'\x00\xff'
Type and identity
type
type(x) returns the type object of x. The built-in type names are these same objects, so type(x) is int holds, and calling one constructs a value. For a user instance the result is its class object.
<class 'int'> True [4, 5] True
Functions, type objects, and classes expose __name__, the bare declared name. On an exception instance, type(e).__name__ gives the exception’s class name.
greet int ZeroDivisionError
object
object() returns a unique featureless instance. Use it as a sentinel. Every value is an instance of object.
False True True
isinstance
isinstance(obj, t) tests membership. t is a built-in type, exception class, user class, or a tuple of those. bool counts as int. Exception classes follow the standard hierarchy. User classes walk their inheritance chain. object matches every value.
True True True
issubclass
issubclass(C, B) tests inheritance. B may be a tuple of classes. C must itself be a class or the call raises TypeError. bool is a subclass of int, and exception classes follow the standard hierarchy.
True True True False
callable
callable(x) is True for functions, lambdas, bound methods, type objects, built-in functions, and instances whose class defines __call__. False for everything else.
True True False
id, hash
id(x) returns a stable numeric identifier for the value. hash(x) returns the hash of a hashable value. Lists, dicts, and sets are unhashable and raise TypeError. Ints hash to themselves. Integral floats hash as the equal int, so hash(1) == hash(1.0).
True True True unhashable
Representation
repr
repr(x) returns the developer-readable form. Strings are quoted and containers show the repr of their elements.
'hello' 42 [1, 'two', 3]
format
format(value) returns the display form. format(value, spec) applies the format spec mini-language from f-strings.
42 00042 3.14 0xff
Attributes
getattr(obj, name) reads an attribute, looking in the instance __dict__, then the class chain, then the built-in method table. A missing name raises AttributeError unless a third argument gives a default.
hasattr(obj, name) runs the same lookup and returns a boolean.
setattr(obj, name, value) writes an attribute on a user instance, class, or function. Built-in types have no writable attributes.
delattr(obj, name) removes an attribute. A missing name raises AttributeError on an instance and is silently ignored on a class.
42 default False
vars
vars(x) returns a snapshot of the attribute dict of an instance or module. There is no no-argument form. Use locals() instead.
{'x': 1, 'y': 2}globals, locals
globals() returns a fresh dict of the module-level bindings. User names only, since built-ins live in a separate namespace. locals() returns a fresh dict of the current frame’s locals inside a function, and matches globals() at module level. Both are copies. Mutating them does not change bindings.
100
7
{'b': 2, 'a': 1}Modules
import_module
import_module(name) returns a module that was imported statically somewhere in the program. It is a lookup, not a load. Every reachable module is still resolved and verified at compile time. An unknown name raises NameError. A name bound to a non-module global, such as a function, raises TypeError.
True 3
Dynamic loading through importlib or __import__ does not exist. Static imports plus import_module replace it.
Classes
super
super() takes no arguments and must be called inside a method. It returns a proxy that resolves attributes against the bases of the current class, starting one step up. See Inheritance and super().
ab
property
property(fget, fset=None) builds a descriptor for a class member. Usually applied through @property with an optional @<name>.setter. See Properties.
9
staticmethod, classmethod
staticmethod(func) wraps a class member so it receives no implicit self. classmethod(func) wraps one so it receives the class as its first argument. Usually applied as decorators. See Static methods and Class methods.
5 9 Math
Async
These functions drive coroutines. Async owns the full model.
run(*coros)runs every argument to completion and returns the first argument’s result. Errors from the other coroutines are discarded.gather(*coros)runs every argument and returns a list of results in argument order. The first error propagates.sleep(seconds)suspends for the duration. A negative value clamps to zero.with_timeout(seconds, coro)returns the coroutine’s result or raisesTimeoutErrorat the deadline.cancel(coro)flags a coroutine for cancellation at its next step.frame()suspends until the host’s next render frame.receive()pops the oldest queued host message.
[2, 4, 6] 42