Unpacking, *args & **kwargs
Spread in TypeScript, * and ** in Python
Section titled “Spread in TypeScript, * and ** in Python”TypeScript’s ... spread operator handles both arrays and objects. Python splits the concept into two operators: * for sequences (lists, tuples, iterables) and ** for dictionaries. The * operator maps closely to what you already know from TypeScript destructuring and spread. The ** operator for dicts has no direct TypeScript equivalent — it is Python-specific, and it is also the syntax for variadic keyword arguments in function signatures.
Tuple unpacking
Section titled “Tuple unpacking”Python lets you unpack any iterable into variables in a single assignment. This is more powerful than TypeScript destructuring because it works on any iterable, not just arrays and objects.
// TypeScript: array destructuringconst [x, y, z] = [1, 2, 3];console.log(x, y, z); // 1 2 3
// Rest elementconst [first, ...rest] = [10, 20, 30, 40, 50];console.log(first, rest); // 10 [20, 30, 40, 50]
// Swap with destructuringlet a = 5, b = 10;[a, b] = [b, a];console.log(a, b); // 10 5# Python: tuple unpackingx, y, z = 1, 2, 3print(x, y, z) # 1 2 3
# Starred assignment (captures middle or end)first, *middle, last = [10, 20, 30, 40, 50]print(first, middle, last) # 10 [20, 30, 40] 50
# Swap without a temp variablea, b = 5, 10a, b = b, aprint(a, b) # 10 5Python’s starred assignment (*middle) is more flexible than TypeScript’s rest element — you can place it anywhere (first, middle, or last position), and Python figures out what goes where.
*args: variadic positional arguments
Section titled “*args: variadic positional arguments”*args in a function signature collects any number of positional arguments into a tuple. The name args is a convention — the * is what matters.
// TypeScript: rest parametersfunction sumAll(...args: number[]): number { return args.reduce((acc, n) => acc + n, 0);}
console.log(sumAll(1, 2, 3, 4, 5)); // 15
// Spreading an array into a function callconst nums = [1, 2, 3];console.log(Math.max(...nums)); // 3# Python: *args collects positional arguments into a tupledef sum_all(*args): return sum(args)
print(sum_all(1, 2, 3, 4, 5)) # 15
# Spreading a list into a function call with *nums = [1, 2, 3]print(max(*nums)) # 3**kwargs: variadic keyword arguments
Section titled “**kwargs: variadic keyword arguments”TypeScript has no built-in equivalent to **kwargs. The closest pattern is accepting an options object and destructuring it. In Python, **kwargs collects any keyword arguments not matched by named parameters into a dict.
// TypeScript: simulate kwargs with an options objectfunction greet(options: Record<string, string | number>): void { for (const [key, val] of Object.entries(options)) { console.log(`${key}: ${val}`); }}
greet({ name: "Alice", age: 30, city: "Bangkok" });
// Object spread / mergeconst defaults = { color: "blue", size: "medium" };const overrides = { color: "red", weight: "heavy" };const merged = { ...defaults, ...overrides };console.log(merged);# Python: **kwargs collects keyword arguments into a dictdef greet(**kwargs): for key, val in kwargs.items(): print(f"{key}: {val}")
greet(name="Alice", age=30, city="Bangkok")
# Dict merge with **defaults = {"color": "blue", "size": "medium"}overrides = {"color": "red", "weight": "heavy"}merged = {**defaults, **overrides}print(merged)Try it
Section titled “Try it”# Tuple unpackingx, y, z = 1, 2, 3print(x, y, z)
# Starred assignmentfirst, *middle, last = [10, 20, 30, 40, 50]print(first, middle, last)
# Swap without temp variablea, b = 5, 10a, b = b, aprint(a, b)
def sum_all(*args): return sum(args)
print(sum_all(1, 2, 3, 4, 5))
def greet(**kwargs): for key, val in kwargs.items(): print(f"{key}: {val}")
greet(name="Alice", age=30, city="Bangkok")
defaults = {"color": "blue", "size": "medium"}overrides = {"color": "red", "weight": "heavy"}merged = {**defaults, **overrides}print(merged)Loading Python runtime (first run only)…