Content · Programming · Python

Python Generic Functions with Type Parameters

A generic function can declare a type parameter directly in its definition, such as def first_item[ItemType](.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A generic function can declare a type parameter directly in its definition, such as def first_item[ItemType](.

What It Does and Why It Matters


A generic function can declare a type parameter directly in its definition, such as def first_item[ItemType](...). A type checker preserves the relationship between input and output types.

Annotations guide static tools; by default they do not validate external input or change the return value at runtime.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

def first_item[ItemType](items: list[ItemType]) -> ItemType:
    if not items:
        raise ValueError('items must not be empty')
    return items[0]
print(first_item([10, 20]))
print(first_item(['blue', 'green']))

How the Code Works


  • The definition of first_item creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

10
blue

Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.

Practice Exercise


Write a generic function that returns the last item from a nonempty tuple.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Assuming the generic annotation checks list contents or adds runtime conversion.
  • Checking only the easiest input and missing a relevant error or boundary case.

Key Takeaways


  • A generic function can declare a type parameter directly in its definition, such as def first_item[ItemType](.
  • A generic function can declare a type parameter directly in its definition, such as def first_item[ItemType](...).
  • Write a generic function that returns the last item from a nonempty tuple.
  • Annotations guide static tools; by default they do not validate external input or change the return value at runtime.
Share this resourceSend this page to someone who may find it useful.