liveBook · Manning

Type signature

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A function signature is the part of a function definition that specifies its callable name and parameters.

A function signature gives callers the function’s name and parameters: the shape of a call. Arguments are the values supplied by a caller; parameters are the names in the definition that receive them. In a Python API, parameter order, keyword names, and defaults all affect how easy that call is to understand and use. section 3.1.2 section 3.2.2

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

The signature of `print` shows a flexible call: `*objects` accepts multiple values, while `sep`, `end`, `file`, and `flush` have defaults that callers can change by name. section 3.1.2

Choose a signature for the calls people will actually make. `full_name(fname, mname, lname)` permits positional calls, where order matters, and keyword calls, where names make the inputs explicit. Making `mname` optional with `full_name(fname, mname=None, lname=None)` also permits a call such as `full_name("John", lname="Smith")`. A `**kwargs` parameter offers more flexibility, but can hide which keywords the function expects unless they are documented. section 3.2.2 section 3.2.2

Across related functions, keep a shared input in the same position and give it the same name when possible. Functions that work on `user_data`, for example, are easier to read together when each takes that dictionary first. The same principle applies to shared file handles or database resources. section 3.2.2

Extend behavior without changing every call

Suppose existing code calls `get_name(person)` to format email greetings. Adding a `tone` parameter would require updating those calls. Separate modules can instead provide formal, informal, and casual implementations with the same `get_name(person)` signature; the application selects the appropriate function while its later call stays unchanged. section 2.3.5

A useful signature makes expected inputs visible, keeps related calls predictable, and avoids unnecessary changes to callers. Here, “signature” concerns the callable name and parameters, not a formal statement of input and return types. section 3.1.2 section 2.3.5

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works through parameter forms and design choices, stopping short of a formal treatment of input and return types.
Examples
Uses print’s defaults, full_name calls, and functions sharing user_data to show how parameter choices affect callers.
What it assumes
Assumes familiarity with Python functions, calls, dictionaries, and modules while distinguishing arguments from parameters.
What you can do after
Apply it to design consistent Python APIs and preserve existing calls when extending behavior through namespaces.
How it connects
Links signatures to keyword arguments, default parameters, variadic parameters, API design, and module namespaces.

Also explained in

Compare how 8 books explain type signature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.