⚙️ Functions & Lambda

Functions & Lambda: A function is like a "make tea" recipe: boil water, steep, pour. Write the recipe once, then every time you want tea you just CALL it — no rewriting the steps

A function is like a "make tea" recipe: boil water, steep, pour. Write the recipe once, then every time you want tea you just CALL it — no rewriting the steps. But a real QA trap hides right here: write def add_test_case(case, cases=[]) and that [] default is created ONLY ONCE (when the function is defined) and SHARED across every call — meaning if one test appends to that list, the next call expecting "an empty list" finds leftovers from the previous one. Java has NO such problem, because default values are re-evaluated on every call. The fix: use cases=None, then inside the function do "if cases is None: cases = []" to build a FRESH list each time — this is one of the sneakiest Python-specific bug patterns out there. So why did Python pick such a "dangerous" design — wouldn't re-evaluating the default on every call be safer? It would, but in Python a def line is not a declaration, it is an EXPRESSION that runs: the moment the function is defined, its defaults are evaluated once and attached to the function object. That is the price of the language's consistency — and the same rule is what lets you compute an expensive value once and hold it as a default. For QA the trap has a familiar face: tests that pass individually break when the whole file runs, and you go looking for "test ordering problems" — when in fact a shared default list leaked one test's data into the next. It is also exactly why pytest rebuilds fixtures for every test: that is the same problem solved at framework level.

Java methods need access modifier and return type: public String greet(String name). Python just needs "def": def greet(name):. Type hints are optional but recommended.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: Python coding practice

Read inputs and data types

Complete the critical line with the smallest change

Run the code and compare expected output

If it fails, read traceback or assertion message

Make the result reusable for a test report

When calling run_test(url, method="GET", timeout=30), in what order does Python match the parameters?

Positional arguments are matched first, in order

Then keyword arguments are matched by name

Parameters still unmatched fall back to their default values from the definition