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
Class-based dependencies in FastAPI
📖 Scenario: You are building a simple FastAPI app that uses a class to manage a counter. This counter will be shared as a dependency in your API endpoints.
🎯 Goal: Create a FastAPI app with a class-based dependency that tracks a counter value. Use this dependency in an endpoint to return the current count.
📋 What You'll Learn
Create a class called Counter with an attribute count initialized to 0
Add a method increment in Counter that increases count by 1
Create a FastAPI app instance called app
Use Depends to inject the Counter class as a dependency in an endpoint
Create a GET endpoint /count that calls increment and returns the current count
💡 Why This Matters
🌍 Real World
Class-based dependencies help organize and reuse logic that needs to maintain state or configuration in web APIs.
💼 Career
Understanding class-based dependencies is important for building scalable and maintainable FastAPI applications in professional backend development.
Progress0 / 4 steps
1
Create the Counter class
Create a class called Counter with an attribute count set to 0 inside the __init__ method.
FastAPI
Hint
Define a class with class Counter:. Inside, create def __init__(self): and set self.count = 0.
2
Add increment method to Counter
Add a method called increment inside the Counter class that increases self.count by 1.
FastAPI
Hint
Define def increment(self): and inside it add self.count += 1.
3
Create FastAPI app and dependency
Import FastAPI and Depends from fastapi. Create an app instance called app. Define a function get_counter that returns a new Counter instance.
FastAPI
Hint
Import FastAPI and Depends. Create app = FastAPI(). Define get_counter() that returns Counter().
4
Create endpoint using class-based dependency
Create a GET endpoint /count using @app.get("/count"). Add a parameter counter with type Counter and default value Depends(get_counter). Inside the function, call counter.increment() and return a dictionary with key count and value counter.count.
FastAPI
Hint
Use @app.get("/count") to create the endpoint. Add counter: Counter = Depends(get_counter) as parameter. Call counter.increment() and return {"count": counter.count}.
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?