Containers¶
Element types inferred from first use, and the difference between mutating a container the function made and one it was given.
Run it¶
What it prints¶
python containers.ppy, ppy containers.ppy, ppy run containers.ppy
Element types from first use¶
@ppy.pure
def grow(count: int) -> list[int]:
out = []
for i in range(count):
out.append(i * i)
return out
out = []has no element type untilout.append(i * i)makes it alist[int].seen = set()becomes aset[str]atseen.add(value).countsis declareddict[int, int], andcounts.get(value, 0) + 1checks against it.
All four functions lower to native code: plain lists, dicts, and sets are
native (see Lists, dicts, and sets).
Python calls histogram and grow natively. It calls distinct and
flatten on their Python bodies: neither writes through its parameter, so
the boundary would read the list in place rather than copy it, but each
does one operation per string or per row (seen.add(value) hashes a
string, out.extend(row) copies a row), and holding each string or row
natively costs about what the native loop saves. main stays in Python,
because sorted(...items()) sorts (key, count) tuples, which native code
does not order. All of them are strict and typed, and all but main are
pure.
Local mutation is pure, shared mutation is not¶
flatten extends a list it created, so it is pure. Had it extended rows,
the write would be an effect on an argument and @ppy.pure would fail with
E1601.
The distinction is by alias, not by name: ys = xs; ys.append(1) mutates
xs whatever it is called, and the analysis follows the alias to say so.
The same alias map is what lets ppy convert declare a read-only parameter
as Sequence[T] rather than list[T].
Where the code comes from¶
containers.ppy is hand-written; there is no .py source and no conversion
step.
Read on: Conversion and inference ยท Effects and purity
17_containers/containers.ppy¶
import ppy
@ppy.pure
def histogram(values: list[int]) -> dict[int, int]:
counts: dict[int, int] = {}
for value in values:
counts[value] = counts.get(value, 0) + 1
return counts
@ppy.pure
def grow(count: int) -> list[int]:
out = []
for i in range(count):
out.append(i * i)
return out
@ppy.pure
def distinct(values: list[str]) -> int:
seen = set()
for value in values:
seen.add(value)
return len(seen)
@ppy.pure
def flatten(rows: list[list[int]]) -> list[int]:
out: list[int] = []
for row in rows:
out.extend(row)
return out
def main() -> None:
print(sorted(histogram([1, 2, 2, 3, 3, 3]).items()))
print(grow(6))
print(distinct(["a", "b", "a", "c"]))
print(flatten([[1, 2], [3], [], [4, 5]]))
main()
Source: examples/17_containers.