You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-16Lines changed: 15 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,7 +96,7 @@ We'll use an in-memory SQLite database for our example:
96
96
97
97
```
98
98
99
-
And make up an imaginary RequestHandler class that uses the SQLite connection:
99
+
And make up an imaginary `RequestHandler` class that uses the SQLite connection:
100
100
101
101
102
102
```pycon
@@ -130,7 +130,7 @@ Key is used to uniquely identifies the configuration dictionary. Next, we bind t
130
130
131
131
```
132
132
133
-
Next we create a module that initialises the DB. It depends on the configuration provided by the above module to create a new DB connection, then populates it with some dummy data, and provides a Connection object:
133
+
Next we create a module that initialises the DB. It depends on the configuration provided by the above module to create a new DB connection, then populates it with some dummy data, and provides a `Connection` object:
134
134
135
135
136
136
```pycon
@@ -149,7 +149,7 @@ Next we create a module that initialises the DB. It depends on the configuration
149
149
150
150
(Note how we have decoupled configuration from our database initialisation code.)
151
151
152
-
Finally, we initialise an Injector and use it to instantiate a RequestHandler instance. This first transitively constructs a sqlite3.Connection object, and the Configuration dictionary that it in turn requires, then instantiates our \`RequestHandler\`:
152
+
Finally, we initialise an `Injector` and use it to instantiate a `RequestHandler` instance. This first transitively constructs a `sqlite3.Connection` object, and the Configuration dictionary that it in turn requires, then instantiates our `RequestHandler`:
153
153
154
154
155
155
```pycon
@@ -160,7 +160,7 @@ Finally, we initialise an Injector and use it to instantiate a RequestHandler in
160
160
161
161
```
162
162
163
-
We can also veryify that our Configuration and SQLite connections are indeed singletons within the Injector:
163
+
We can also veryify that our `Configuration` and `SQLite` connections are indeed singletons within the Injector:
164
164
165
165
166
166
```pycon
@@ -174,7 +174,7 @@ True
174
174
You're probably thinking something like: "this is a large amount of work just to give me a database connection", and you are correct; dependency injection is typically not that useful for smaller projects. It comes into its own on large projects where the up-front effort pays for itself in two ways:
175
175
176
176
1. Forces decoupling. In our example, this is illustrated by decoupling our configuration and database configuration.
177
-
2. After a type is configured, it can be injected anywhere with no additional effort. Simply @inject and it appears. We don't really illustrate that here, but you can imagine adding an arbitrary number of RequestHandler subclasses, all of which will automatically have a DB connection provided.
177
+
2. After a type is configured, it can be injected anywhere with no additional effort. Simply `@inject` and it appears. We don't really illustrate that here, but you can imagine adding an arbitrary number of `RequestHandler` subclasses, all of which will automatically have a DB connection provided.
178
178
179
179
Terminology
180
180
-----------
@@ -341,36 +341,35 @@ is equivalent to:
341
341
342
342
```
343
343
344
-
**Note**: You can also begin the name of injected member with an underscore(s) (to indicate the member being private for example). In such case the member will be injected using the name you specified, but corresponding parameter in a constructor (let's say you instantiate the class manually) will have the underscore prefix stripped (it makes it consistent with most of the usual parameter names):
344
+
**Note 1**: You can also begin the name of injected member with an underscore(s) (to indicate the member being private for example). In such case the member will be injected using the name you specified, but corresponding parameter in a constructor (let's say you instantiate the class manually) will have the underscore prefix stripped (it makes it consistent with most of the usual parameter names):
345
345
346
346
347
347
```pycon
348
348
>>> @inject(_y=int)
349
349
... classX(object):
350
350
... pass
351
-
352
-
```
353
-
354
-
355
-
```pycon
351
+
...
356
352
>>> x1 = injector.get(X)
357
353
>>> x1.y
358
354
Traceback (most recent call last):
359
355
AttributeError: 'X' object has no attribute 'y'
360
356
>>> x1._y
361
357
0
362
-
363
-
```
364
-
365
-
366
-
```pycon
367
358
>>> x2 = X(y=2)
368
359
>>> x2.y
369
360
Traceback (most recent call last):
370
361
AttributeError: 'X' object has no attribute 'y'
371
362
>>> x2._y
372
363
2
364
+
```
365
+
366
+
**Note 2**: When class is decorated with `inject` decorator you need to use keyword arguments when instantiating the class manually:
373
367
368
+
```pycon
369
+
>>> Item(name='computer') # that's ok
370
+
<Item object at 0x10cb04210>
371
+
>>> Item('computer') # that'll result in a CallError
0 commit comments