Skip to content

Commit ef1f9cb

Browse files
committed
Update README.md
1 parent fd4ee33 commit ef1f9cb

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ We'll use an in-memory SQLite database for our example:
9696

9797
```
9898

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:
100100

101101

102102
```pycon
@@ -130,7 +130,7 @@ Key is used to uniquely identifies the configuration dictionary. Next, we bind t
130130

131131
```
132132

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:
134134

135135

136136
```pycon
@@ -149,7 +149,7 @@ Next we create a module that initialises the DB. It depends on the configuration
149149

150150
(Note how we have decoupled configuration from our database initialisation code.)
151151

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`:
153153

154154

155155
```pycon
@@ -160,7 +160,7 @@ Finally, we initialise an Injector and use it to instantiate a RequestHandler in
160160

161161
```
162162

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:
164164

165165

166166
```pycon
@@ -174,7 +174,7 @@ True
174174
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:
175175

176176
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.
178178

179179
Terminology
180180
-----------
@@ -341,36 +341,35 @@ is equivalent to:
341341

342342
```
343343

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):
345345

346346

347347
```pycon
348348
>>> @inject(_y=int)
349349
... class X(object):
350350
... pass
351-
352-
```
353-
354-
355-
```pycon
351+
...
356352
>>> x1 = injector.get(X)
357353
>>> x1.y
358354
Traceback (most recent call last):
359355
AttributeError: 'X' object has no attribute 'y'
360356
>>> x1._y
361357
0
362-
363-
```
364-
365-
366-
```pycon
367358
>>> x2 = X(y=2)
368359
>>> x2.y
369360
Traceback (most recent call last):
370361
AttributeError: 'X' object has no attribute 'y'
371362
>>> x2._y
372363
2
364+
```
365+
366+
**Note 2**: When class is decorated with `inject` decorator you need to use keyword arguments when instantiating the class manually:
373367

368+
```pycon
369+
>>> Item(name='computer') # that's ok
370+
<Item object at 0x10cb04210>
371+
>>> Item('computer') # that'll result in a CallError
372+
Traceback (...)
374373
```
375374

376375
### Injector

0 commit comments

Comments
 (0)