[Baypiggies] Are there any good examples of implementing dependency inversion in Python?

Braun Brelin bbrelin at gmail.com
Mon Feb 27 05:14:28 EST 2017


Part of the issue, I think is that Python's duck typing obviates the need
for many (but not all) of the design patterns that are bandied about,
including dependency inversion.

Braun


On Mon, Feb 27, 2017 at 1:30 AM, Alex Martelli <aleax at google.com> wrote:

> On Sun, Feb 26, 2017 at 4:04 PM, Mahmoud Hashemi <mahmoud at hatnote.com>
> wrote:
>
>> It was my impression that inversion and injection were really closely
>> related, based on
>>
>
> As I said, related, but not the same thing. Dependency injection, as
> covered in my talk, has uses quite apart from inversion of control -- the
> example my talk best covers is how to make a scheduler that can cleanly be
> unit-tested and/or integrated with some event loop. Mocking (e.g
> unittest.mock.patch) can often, so to speak, force a dependency injection
> to test code that's not designed with DI in mind -- but, as usual,
> "explicit is better than implicit" (clearer and, so to speak, `sharper`)...
> and you probably wouldn't want to use such patching outside of a unit-test
> (though I did once use it to most-simply graft a `-n` mode [as in `make
> -n`] onto an existing utility -- "display all commands you'd execute but
> don't execute them"... Michael Foord was quite amused when I challenged him
> to come up with a good non-unit-test use of his unittest.mock code, then
> revealed that one!-).
>
>
> Alex
>
>
>> various Fowler talks, including this canonical essay:
>> https://martinfowler.com/articles/injection.html
>>
>> My main takeaway has always been, pass the low-level implementation
>> _into_ the higher level construct, instead of expecting the higher-level
>> wrapper to create it for you. This is especially visible in Twisted's
>> Protocol-based approach, now visible in asyncio, and more broadly all the
>> sans-io libraries: http://sans-io.readthedocs.io/
>>
>> For a very basic example, consider this JSONLines (jsonlines.org)
>> iterator: http://boltons.readthedocs.io/en/latest/jsonutils.html
>>
>> Rather than doing JSONLIterator('target.jsonl') directly, one would do
>> JSONLIterator(open('target.jsonl')), passing in the open file object (or
>> any file-like object). Hope that makes sense!
>>
>> Mahmoud
>>
>>
>>
>> On Sun, Feb 26, 2017 at 3:54 PM, Alex Martelli via Baypiggies <
>> baypiggies at python.org> wrote:
>>
>>> I see you're getting answers on dependency *injection* (on which I would
>>> suggest my talk, slides at http://www.aleax.it/yt_pydi.pdf --
>>> unfortunately I don't know of a video recording of said talk) while you
>>> asked about dependency *inversion* (related, but definitely not the same
>>> thing), for which I don't know of much that's published apart from what a
>>> search will easily find, such as https://www.lynda.com/Progr
>>> amming-Languages-tutorials/Introduction-dependency-inversion
>>> /471978/502206-4.html .
>>>
>>> Alex
>>>
>>> On Sun, Feb 26, 2017 at 12:55 PM, Braun Brelin <bbrelin at gmail.com>
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> Looking for some good examples of Dependency Inversion in Python.
>>>> Anyone have any suggestions for sites to look at?
>>>>
>>>> Thanks,
>>>>
>>>> _______________________________________________
>>>> Baypiggies mailing list
>>>> Baypiggies at python.org
>>>> To change your subscription options or unsubscribe:
>>>> https://mail.python.org/mailman/listinfo/baypiggies
>>>>
>>>
>>>
>>> _______________________________________________
>>> Baypiggies mailing list
>>> Baypiggies at python.org
>>> To change your subscription options or unsubscribe:
>>> https://mail.python.org/mailman/listinfo/baypiggies
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20170227/aba9e013/attachment-0001.html>


More information about the Baypiggies mailing list