News for hackers. Stuff that matters.
Re:Printf(“%s %s”, dependency, injection) by Spivak on Tuesday October 19, 2021 @03:10PM (#28922260)
Python also has a really nice pattern using classes for this. I've heard it called the supply chain pattern before.

    class MyClient(HTTPClient):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        def do_some_networking(self, arg):
            super().http_get("server", arg)

    class MyClientWithDI(MyClient, MyHTTPClient):
        pass

    client = MyClientWithDI()
    # Will call MyHTTPClient's http_get
    client.do_some_networking("/status")
Re:Printf(“%s %s”, dependency, injection) by nerdponx on Tuesday October 19, 2021 @06:51PM (#28924732)
I've actually never seen this one before, and I'm not sure if I like it. I generally tend to favor "composition over inheritance", and I'm not sure what the advantage of inheritance is in this case, compared to standard DI.
Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @08:27AM (#28929822)
Python's threads used to work like that, for example. (They still have that as an option, but I think the composition is more preferred these days?)
Re:Printf(“%s %s”, dependency, injection) by nerdponx on Wednesday October 20, 2021 @09:10AM (#28930229)
The old-school way to use Thread is to subclass it and override some methods. The post here is describing a setup where you don't override methods, but instead provide some kind of mixin. I've never seen anyone write this, for example:

    class MyThreadImpl:
        def run(self):
            do_stuff()

    class MyThread(Thread, MyThreadImpl):
        pass