EasyPython

Limit Function Arguments

PythonClean CodeFunctionsRefactoring

Functions with many parameters are hard to call correctly — which order do the args go?

# dirty — 5 positional arguments
def solve(first_name, last_name, age, email, role):
    return {'first_name': first_name, 'last_name': last_name, 'age': age, 'email': email, 'role': role}

Refactor so solve accepts a single options dict instead of 5 positional arguments.

solve({'first_name': 'Alice', 'last_name': 'Smith', 'age': 30, 'email': 'a@b.com', 'role': 'admin'})

Sample tests

Test #1Returns the same dict shape
Input: [{"age":30,"role":"admin","email":"a@b.com","last_name":"Smith","first_name":"Alice"}]
Output: {"age":30,"role":"admin","email":"a@b.com","last_name":"Smith","first_name":"Alice"}
Test #2Works for any valid input
Input: [{"age":25,"role":"user","email":"b@c.com","last_name":"Jones","first_name":"Bob"}]
Output: {"age":25,"role":"user","email":"b@c.com","last_name":"Jones","first_name":"Bob"}
Test #3Empty values pass through unchanged
Input: [{"age":0,"role":"","email":"","last_name":"","first_name":""}]
Output: {"age":0,"role":"","email":"","last_name":"","first_name":""}