Bird
0
0

In FastAPI, how do you declare a dependency that requires a parameter to be passed when used?

easy🧠 Conceptual Q1 of 15
FastAPI - Dependency Injection
In FastAPI, how do you declare a dependency that requires a parameter to be passed when used?
ADefine a function that returns another function accepting the parameter
BUse a global variable to store the parameter value
CPass the parameter directly in the route decorator
DUse a class without __call__ method as dependency
Step-by-Step Solution
Solution:
  1. Step 1: Understand dependency with parameters pattern

    In FastAPI, to pass parameters to dependencies, you create a function that returns another function which accepts the parameter.
  2. Step 2: Recognize correct usage

    This returned function is then used as the actual dependency, allowing parameter passing.
  3. Final Answer:

    Define a function that returns another function accepting the parameter -> Option A
  4. Quick Check:

    Dependency with parameters = Function returning function [OK]
Quick Trick: Use nested functions to pass parameters in dependencies [OK]
Common Mistakes:
MISTAKES
  • Trying to pass parameters directly in route decorator
  • Using global variables for parameters
  • Using classes without __call__ for parameterized dependencies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes