Bird
0
0

You want to create a dependency that multiplies a query parameter by a fixed factor passed when creating the dependency. Which approach is correct?

hard🚀 Application Q9 of 15
FastAPI - Dependency Injection
You want to create a dependency that multiplies a query parameter by a fixed factor passed when creating the dependency. Which approach is correct?
AUse a global factor variable and a dependency that reads the query parameter
BPass the factor as a query parameter directly to the endpoint
CCreate a function that takes factor and returns a dependency function using the query parameter
DUse a class without __call__ method to hold factor and query parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameterized dependency pattern

    To pass a fixed factor, create a function that returns a dependency function capturing that factor.
  2. Step 2: Use query parameter inside returned dependency

    The returned dependency function can accept the query parameter and multiply it by the fixed factor.
  3. Final Answer:

    Create a function that takes factor and returns a dependency function using the query parameter -> Option C
  4. Quick Check:

    Parameterized dependency = function returning dependency function [OK]
Quick Trick: Use closures to capture fixed parameters in dependencies [OK]
Common Mistakes:
MISTAKES
  • Using global variables instead of closures
  • Passing factor as query parameter instead of fixed
  • Using classes without __call__ for dependencies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes