Fairly similar - easy way is to initialize the object you will need in the class and then call directly. class FrameworkClass(): def __init__(self, func): self.func = func def do_the_job(self): # some stuff self.func() def my_func(): # do whatever here
@@codingwithroby yes the vs-code theme in this tutorial. And also i am very confused while learning fastapi there are so many ways in fastapi even doing a single thing like SQL alchemy vs sqlmodel and then using sa_column in sqlmodel to write SQL alchemy in sqlmodel and then there are so many drivers like psycopg3 vs psycopg2 vs asyncpg, and war does not stop there there are multiple sessions classes and multiple ways to use session like you can use it with context manager or with a context decorator. Every time I try to use async in python I get stuck. I thought django is complicated but in programming making a full stack application seems very complicated to me because one thing depends on the other and the other thing depends on something else. I am searching for a resource for learning fastapi like a django-book Django by Examples by antonio mele. Building real-world production grade projects.
Thank You. Why don't you organize you code in `__init__()` in your classes without creating unnecessary functions next to classes? Dependency injection in FastAPI requires callable, so it may be a class, not only function.
I have learned it is easy for beginners to learn by breaking things out like this. But perhaps in the future I should start implementing in the constructor or class like you are suggesting.
ok so whole point is to save resources and not create new instance on every api call ryt? but like when we create a new function which returns the new instance for us won't that fn be called on every api call and return a new instance i might be wrong but it feels like we r doing same thing with extra steps help?. also great content
Nonsense examples. None of those have any real world benefit or even meaning. The whole point of dependency injection in fastapi is to have your dependency function examine, extract or transform any part of the request, since your annotated dependency function arguments can specify parts of the request or the whole request object that fastapi will inject for you when invoking your dependency function, which itself can return a value to inject into your endpoint (or not). These examples literally have zero use in a fastapi endpoint and miss the point 100%.
There it is folks, dependency injection. What’s your biggest challenge as a developer right now?
Great video Eric, I hope to built videos like this soon (to help python community in Spanish), thanks for sharing your knowledge.
You can do it 🙂 Just be ready for a ton of work and a lot of trial and error.
More of these please lol just subbed great explanation
You bet! I got you
well explained sir
Thank you friend 🙂
easy concept for spring boot dev
Dependency injection is all over the place in Spring Boot!
Could you make a video on how to use dependency injection without fastapi?
Fairly similar - easy way is to initialize the object you will need in the class and then call directly.
class FrameworkClass():
def __init__(self, func):
self.func = func
def do_the_job(self):
# some stuff
self.func()
def my_func():
# do whatever here
which theme are you using in this tutorial?
On my VSCode?
@@codingwithroby yes the vs-code theme in this tutorial.
And also i am very confused while learning fastapi there are so many ways in fastapi even doing a single thing like SQL alchemy vs sqlmodel and then using sa_column in sqlmodel to write SQL alchemy in sqlmodel and then there are so many drivers like psycopg3 vs psycopg2 vs asyncpg, and war does not stop there there are multiple sessions classes and multiple ways to use session like you can use it with context manager or with a context decorator. Every time I try to use async in python I get stuck. I thought django is complicated but in programming making a full stack application seems very complicated to me because one thing depends on the other and the other thing depends on something else. I am searching for a resource for learning fastapi like a django-book Django by Examples by antonio mele. Building real-world production grade projects.
Thank You. Why don't you organize you code in `__init__()` in your classes without creating unnecessary functions next to classes? Dependency injection in FastAPI requires callable, so it may be a class, not only function.
I have learned it is easy for beginners to learn by breaking things out like this. But perhaps in the future I should start implementing in the constructor or class like you are suggesting.
I love love love ur videoss❤❤❤
Aww thank you so much 😊
ok so whole point is to save resources and not create new instance on every api call ryt? but like when we create a new function which returns the new instance for us won't that fn be called on every api call and return a new instance i might be wrong but it feels like we r doing same thing with extra steps help?. also great content
Point was modular codebase, if you don't keep seperate function you would copy paste it every where .
Makes easy to understand for everyone.
It's more for scalable and modular codebase, not resource management.
😱8 decade of experience 😅
"A decade of experience" 🙂
For once I thought DI is a security vulnerability like sqli 😅
ohhhh no!!! lol
Me too😂
Nonsense examples. None of those have any real world benefit or even meaning. The whole point of dependency injection in fastapi is to have your dependency function examine, extract or transform any part of the request, since your annotated dependency function arguments can specify parts of the request or the whole request object that fastapi will inject for you when invoking your dependency function, which itself can return a value to inject into your endpoint (or not). These examples literally have zero use in a fastapi endpoint and miss the point 100%.
I appreciate your feedback.