Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a class-based dependency in FastAPI?
A class-based dependency is a Python class used to define reusable logic that FastAPI can inject into path operations. It allows grouping related code and state in one place.
Click to reveal answer
beginner
How do you make a class usable as a dependency in FastAPI?
You define a __call__ method inside the class. FastAPI treats instances of this class as callable dependencies.
Click to reveal answer
intermediate
Why use class-based dependencies instead of functions in FastAPI?
Class-based dependencies help organize related logic and state together, making code cleaner and easier to maintain, especially when you need to share data or setup across multiple calls.
Click to reveal answer
intermediate
How does FastAPI handle the lifecycle of class-based dependencies?
FastAPI creates a new instance of the class for each request by default, calling its __call__ method to resolve the dependency.
Click to reveal answer
advanced
Can class-based dependencies have their own dependencies in FastAPI?
Yes, you can declare dependencies inside the __init__ method or __call__ method, allowing nested dependency injection.
Click to reveal answer
What special method must a class have to be used as a FastAPI dependency?
A__str__
B__call__
C__init__
D__repr__
✗ Incorrect
FastAPI uses the __call__ method to treat the class instance as a callable dependency.
How does FastAPI create instances of class-based dependencies by default?
AOne instance per request
BIt does not create instances automatically
COne instance per application startup
DOne instance shared across all requests
✗ Incorrect
FastAPI creates a new instance of the class for each request to keep dependencies isolated.
Where can you declare nested dependencies inside a class-based dependency?
AIn both __init__ and __call__ methods
BOnly in __init__ method
COnly in __call__ method
DNested dependencies are not supported
✗ Incorrect
You can declare nested dependencies in both __init__ and __call__ methods for flexibility.
What is a main benefit of using class-based dependencies?
AThey avoid using async functions
BThey run faster than function dependencies
CThey require less code
DThey allow grouping related logic and state
✗ Incorrect
Class-based dependencies help organize related code and state together for cleaner design.
Which FastAPI feature allows injecting class-based dependencies into path operations?
ARequest
BResponse
CDepends
DMiddleware
✗ Incorrect
The Depends function is used to declare dependencies, including class-based ones.
Explain how to create and use a class-based dependency in FastAPI.
Think about how FastAPI treats callable classes as dependencies.
You got /4 concepts.
Describe the advantages of using class-based dependencies over function dependencies in FastAPI.
Consider how grouping logic and state helps in bigger projects.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using class-based dependencies in FastAPI?
easy
A. To automatically generate HTML forms
B. To replace all route functions with classes
C. To group related dependency logic in one reusable place
D. To handle database connections only
Solution
Step 1: Understand the role of class-based dependencies
Class-based dependencies allow grouping related logic inside a class, making code cleaner and reusable.
Step 2: Compare options with this purpose
Only To group related dependency logic in one reusable place correctly describes grouping related logic; others describe unrelated features.
Final Answer:
To group related dependency logic in one reusable place -> Option C
Quick Check:
Class-based dependencies = Group logic [OK]
Hint: Class-based dependencies group logic inside a class [OK]
Common Mistakes:
Thinking class dependencies replace route functions
Assuming they auto-generate HTML
Believing they only handle databases
2. Which method must a class implement to be used as a dependency in FastAPI?
easy
A. __init__
B. __call__
C. dependency
D. run
Solution
Step 1: Recall FastAPI dependency requirements
FastAPI requires the class to be callable, which means it must implement the __call__ method.
Step 2: Match method names to this requirement
Only __call__ makes the class instance callable; __init__ is for initialization, others are invalid.
Final Answer:
__call__ -> Option B
Quick Check:
Callable class = __call__ method [OK]
Hint: Class must be callable via __call__ method [OK]
Common Mistakes:
Choosing __init__ instead of __call__
Using random method names like 'run'
Confusing dependency with method name
3. Given this class-based dependency, what will be the output when accessing the endpoint?