Built-in Functions
The functions on this page are always in scope - no import needed. They are the workhorses you reach for every day: printing, measuring length, looping over indices, converting between types, and inspecting values. Dragon mirrors Python's names and signatures wherever the typed, compiled model allows, so most of what you know from Python transfers directly.
Every entry in this appendix was verified by compiling and running it against the Dragon compiler. Where a Python builtin is recognized by the grammar but does not yet have a working implementation, it is listed in Not yet available with the exact error - so you always know what is real.
Output and input
| Function | Signature | Description |
|---|---|---|
print | print(*values) -> None | Writes each value to stdout, space-separated, ending in a newline. |
input | input(prompt: str = "") -> str | Prints the prompt (no newline) and reads one line from stdin. |
print("hello") # hello
print(1, 2, 3) # 1 2 3
const name: str = input("Name: ") # reads a line after printing "Name: "
print("Hi " + name)
Length and ranges
| Function | Signature | Description |
|---|---|---|
len | len(x) -> int | Number of elements in a str, list, dict, set, tuple, or bytes. |
range | range(stop) / range(start, stop) / range(start, stop, step) | A lazy sequence of integers for iteration. |
print(len("abc")) # 3
print(len([1, 2, 3])) # 3
for i in range(5) {
print(i) # 0 1 2 3 4
}
range is an iteration construct, not an annotation type - write for i in range(n), not r: range = range(n). To materialize the values into a list, use a comprehension: const xs: list[int] = [i for i in range(5)].
Type conversion
| Function | Signature | Description |
|---|---|---|
int | int(x) -> int | Parses a str, or truncates a float, to an integer. |
float | float(x) -> float | Parses a str, or widens an int, to a float. |
str | str(x) -> str | The human-readable text form of any value (incl. lists, dicts). |
bool | bool(x) -> bool | Truthiness of a value (see the note below). |
bytes | bytes() / bytes(n: int) / bytes(xs: list[int]) | Empty, n zero bytes, or bytes from a list of ints. |
print(int("17")) # 17
print(int(3.9)) # 3
print(float("2.5")) # 2.5
print(str([1, 2, 3])) # [1, 2, 3]
bytes("text", "utf-8")is not supported - the two-argumentstring-encode form errors. Build bytes via the
bytes(list[int])form or theio / encoding stdlib instead.
Numeric helpers
| Function | Signature | Description |
|---|---|---|
abs | abs(x: int) -> int / abs(x: float) -> float | Absolute value; the result type matches the argument. |
min | min(a, b) / min(xs: list) | Smaller of two values, or the minimum of a list. |
max | max(a, b) / max(xs: list) | Larger of two values, or the maximum of a list. |
sum | sum(xs: list) | Sum of a list of numbers. |
print(abs(-5)) # 5
print(min(3, 1)) # 1
print(max([4, 2, 9])) # 9
print(sum([1, 2, 3, 4])) # 10
Sequence operations
| Function | Signature | Description |
|---|---|---|
sorted | sorted(xs: list[T]) -> list[T] | A new list with the elements in ascending order. |
reversed | reversed(xs: list[T]) -> list[T] | A new list with the elements in reverse order. |
enumerate | enumerate(xs) | Yields (index, value) pairs while iterating. |
zip | zip(a, b) | Yields tuples pairing elements of two sequences. |
any | any(xs: list[bool]) -> bool | True if any element is truthy. |
all | all(xs: list[bool]) -> bool | True if every element is truthy. |
sorted and reversed return a new list[T]:
const s: list[int] = sorted([3, 1, 2])
print(s) # [1, 2, 3]
const r: list[int] = reversed([1, 2, 3])
print(r) # [3, 2, 1]
enumerate and zip are used as loop drivers:
for i, v in enumerate([10, 20, 30]) {
print(i, v) # 0 10 / 1 20 / 2 30
}
for a, b in zip([1, 2, 3], [10, 20, 30]) {
print(a, b) # 1 10 / 2 20 / 3 30
}
print(any([False, True, False])) # True
print(all([True, False])) # False
Introspection
| Function | Signature | Description |
|---|---|---|
isinstance | isinstance(obj, Cls) -> bool | Whether obj is an instance of Cls (or a subclass). |
repr | repr(x) -> str | A debugging representation - strings are quoted. |
id | id(x) -> int | A stable integer identity for the object. |
hash | hash(x) -> int | A hash value for the object. |
chr | chr(code: int) -> str | The one-character string for a code point. |
ord | ord(ch: str) -> int | The code point of a one-character string. |
print(repr("hi")) # 'hi'
print(repr(42)) # 42
print(chr(65)) # A
print(ord("A")) # 65
isinstance works for class hierarchies, checking against a base type:
class Animal {
name: str
def(n: str) { self.name = n }
}
class Dog(Animal) {
def(n: str) { super(n) }
}
const d: Animal = Dog("Rex")
print(isinstance(d, Animal)) # True
isinstanceagainst builtin primitive types returnsFalse.
isinstance(5, int),isinstance("x", str), andisinstance(3.0, float)all return
Falsetoday - it is reliable only for user-defined classhierarchies, and only when the runtime value matches (or subclasses) the
type being tested against the declared base.
Number-base formatting
| Function | Signature | Description |
|---|---|---|
hex | hex(n: int) -> str | Hexadecimal string with a 0x prefix. |
oct | oct(n: int) -> str | Octal string with a 0o prefix. |
bin | bin(n: int) -> str | Binary string with a 0b prefix. |
print(hex(255)) # 0xff
print(oct(8)) # 0o10
print(bin(5)) # 0b101
Collection constructors
| Function | Signature | Description |
|---|---|---|
set | set() / set(xs: list[T]) -> set[T] | An empty set, or a set built from a list (dedupes). |
dict | dict() -> dict[K, V] | An empty dictionary. |
list | list() -> list[Any] | An empty list (typed list[Any]). |
const s: set[int] = set([1, 2, 2, 3])
print(s) # {1, 2, 3}
const d: dict[str, int] = dict()
print(len(d)) # 0
list()always yieldslist[Any]. It cannot be assigned to aconcretely-typed binding such as
list[int](you get
cannot assign 'list[Any]' to variable of type 'list[int]'). For a typedempty list use the literal:
const xs: list[int] = []. Thelist(iterable)copy form is likewise not yet wired - copy with a comprehension,
[x for x in src].
Numeric and functional builtins
These mirror Python's names in the forms the typed model supports. A few carry a caveat worth knowing, noted in the table.
| Function | Signature | Notes |
|---|---|---|
pow | pow(base: int, exp: int) -> int | Integer power; same as base ** exp. |
round | round(x: float) -> int | Rounds to the nearest integer. The 2-arg round(x, ndigits) form is not available. |
divmod | divmod(a: int, b: int) -> tuple[int, int] | (quotient, remainder). |
map | map(fn, xs) -> list | Eager: returns a list, not a lazy iterator. A comprehension is the idiomatic form. |
filter | filter(fn, xs) -> list | Eager: returns a list. |
tuple | tuple(iterable) -> tuple | Builds a tuple from an iterable; a list literal argument gives an exact-arity tuple[...]. |
type | type(x) -> str | Returns the type name, e.g. "int", "str", "list". |
Not yet available
These names are recognized by the grammar (they highlight as builtins) but do not compile-and-run today. Each was tested; the error is shown so you know exactly where the boundary is.
| Function | Status | Error when used |
|---|---|---|
iter, next, getattr, setattr, hasattr, delattr, dir, vars, globals, locals, eval, exec, compile, callable, format, issubclass, slice, frozenset | Reserved, unverified | undefined name '...' / Unknown function: ... |
The dynamic-reflection names (eval, exec, compile, getattr, setattr, vars, globals, locals) are excluded on purpose: they are the dynamic crutches Dragon rejects, not gaps waiting to be filled. A comprehension ([f(x) for x in xs], [x for x in xs if cond]) is still the idiomatic and faster form even though map/filter now work.
At a glance
| You want to... | Write |
|---|---|
| Print values | print(a, b, c) |
| Read a line | const s: str = input("> ") |
| Measure size | len(x) |
| Loop over indices | for i in range(n) { ... } |
| Loop with index | for i, v in enumerate(xs) { ... } |
| Pair two sequences | for a, b in zip(xs, ys) { ... } |
| Convert to text | str(x) |
| Parse a number | int(s) / float(s) |
| Sort | const s: list[T] = sorted(xs) |
| Reverse | const r: list[T] = reversed(xs) |
| Dedupe a list | set(xs) |
| Any / all true | any(flags) / all(flags) |
| Char and code | chr(n) / ord(c) |
| Number base | hex(n) / oct(n) / bin(n) |
| Transform a list | [f(x) for x in xs] or map(f, xs) |
| Filter a list | [x for x in xs if c] or filter(f, xs) |
For the operators (**, //, in, not in, comparison, bitwise) see the operators appendix; for keywords see the keywords appendix; and for everything behind an import see the stdlib appendix.
