]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:memo: Update docs for dependencies
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 5 Jan 2019 13:19:41 +0000 (17:19 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Sat, 5 Jan 2019 13:19:41 +0000 (17:19 +0400)
docs/tutorial/dependencies/advanced-dependencies.md
docs/tutorial/dependencies/classes-as-dependencies.md

index d63170d63227e04d413a7214ed289c003fde2ac4..8240064412b086799abddf6e26c77bb36a3a4944 100644 (file)
@@ -25,6 +25,8 @@ To do that, we declare a method `__call__`:
 {!./src/dependencies/tutorial006.py!}
 ```
 
+In this case, this `__call__` is what **FastAPI** will use to check for additional parameters and sub-dependencies, and this is what will be called to pass a value to the parameter in your *path operation function* later.
+
 ## Parameterize the instance
 
 And now, we can use `__init__` to declare the parameters of the instance that we can use to "parameterize" the dependency:
index e5ce515280e401f3438e4e0d79e4136ff61d004e..44635ed0b33ddd71355dcd961e02b994079c2413 100644 (file)
@@ -10,7 +10,7 @@ In the previous example, we where returning a `dict` from our dependency ("depen
 
 But then we get a `dict` in the parameter `commons` of the path operation function.
 
-And we know that `dict`s can't provide a lot of editor support because they can't know their keys and value types.
+And we know that editors can't provide a lot of support (like completion) for `dict`s, because they can't know their keys and value types.
 
 We can do better...
 
@@ -24,7 +24,7 @@ The key factor is that a dependency should be a "callable".
 
 A "**callable**" in Python is anything that Python can "call" like a function.
 
-So, if you have an object `something` (that might _not_ be a function) and you can do:
+So, if you have an object `something` (that might _not_ be a function) and you can "call" it (execute it) like:
 
 ```Python
 something()
@@ -42,6 +42,21 @@ then it is a "callable".
 
 You might notice that to create an instance of a Python class, you use that same syntax.
 
+For example:
+
+```Python
+class Cat:
+    def __init__(self, name: str):
+        self.name = name
+
+
+fluffy = Cat(name="Mr Fluffy")
+```
+
+In this case, `fluffy` is an instance of the class `Cat`.
+
+And to create `fluffy`, you are "calling" `Cat`.
+
 So, a Python class is also a **callable**.
 
 Then, in **FastAPI**, you could use a Python class as a dependency.
@@ -50,7 +65,7 @@ What FastAPI actually checks is that it is a "callable" (function, class or anyt
 
 If you pass a "callable" as a dependency in **FastAPI**, it will analyze the parameters for that "callable", and process them in the same way as the parameters for a path operation function. Including sub-dependencies.
 
-That also applies to callables with no parameters at all. The same as would be for path operation functions with no parameters.
+That also applies to callables with no parameters at all. The same as it would be for path operation functions with no parameters.
 
 Then, we can change the dependency "dependable" `common_parameters` from above to the class `CommonQueryParameters`: