DEV Community


Discussion on: A Pythonic Guide to SOLID Design Principles

brycepg profile image
Bryce Guinta

What do you think about dependency injecting the FTPDriver itself so that the IO (establishing connection) isn't in the constructor? Example:

class FTPClient:
    def __init__(self, driver):
        self._driver = driver

    def from_connection(cls, host, port):
        driver = FTPDriver(host, port)
        return cls(driver)

This allows one to install a TestDriver for testing FTPClient. and bubbles up IO to an outer layer which uncle bob is big on (, but we get to do it in one class because Python supports classmethods.

ezzy1337 profile image
Derek D. Author

It's an excellent practice. I left it out so the examples were more concrete and demonstrated the principles as simply as possible.