Bird
0
0

Which syntax correctly declares a class-based dependency in FastAPI?

easy📝 Syntax Q3 of 15
FastAPI - Dependency Injection
Which syntax correctly declares a class-based dependency in FastAPI?
Aclass MyDep: def dependency(self): return 'value'
Bdef MyDep(): return 'value'
Cclass MyDep: async def __call__(self): return 'value'
Dclass MyDep: def __init__(self): return 'value'
Step-by-Step Solution
Solution:
  1. Step 1: Check correct class-based dependency syntax

    FastAPI expects a class with an async or sync __call__ method returning the dependency value.
  2. Step 2: Analyze options

    Only class MyDep: async def __call__(self): return 'value' defines __call__ correctly; others are functions or invalid class methods.
  3. Final Answer:

    class MyDep:\n async def __call__(self):\n return 'value' -> Option C
  4. Quick Check:

    Class with __call__ method = C [OK]
Quick Trick: Use __call__ method inside class for dependency [OK]
Common Mistakes:
MISTAKES
  • Using regular methods instead of __call__
  • Returning values from __init__ method
  • Confusing functions with class dependencies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes