DEV Community

loading...

Discussion on: A Pythonic Guide to SOLID Design Principles

Collapse
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

    @classmethod
    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 (blog.cleancoder.com/uncle-bob/2012...), but we get to do it in one class because Python supports classmethods.

Collapse
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.