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?
✗ 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?
✗ 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?
✗ Incorrect
You can declare nested dependencies in both __init__ and __call__ methods for flexibility.
What is a main benefit of using class-based dependencies?
✗ 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?
✗ 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.