r/learnpython•
Synlis

Which is the best way to instantiate a dataclass that depends on an imported class?

I was reading this post that explains why leveraging dataclass is beneficial in python: https://blog.glyph.im/2025/04/stop-writing-init-methods.html#fn:2:stop-writing-init-methods-2025-4

Now, I was trying to put it in practice with a wrapper around a paramiko ssh client. But I am at a loss on which way would be better:

  1. Be a subclass of paramiko SSHClient:
@dataclass
class SSHClient(paramiko.SSHClient):
    """
        Minimal wrapper around paramiko.SSHClient to set some config by default.
    """
    _hostname: str
    _user: str

    @classmethod
    def create_connection(cls, hostname: str, user: str = "") -> Self:
        self = cls.__new__(cls)
        super(cls, self).__init__()

        self._hostname = hostname
        self._user = user

        super(cls, self).connect(hostname, username=user)

        return self
  1. Be its own class with a class variable of type paramiko.SSHClient:
@dataclass
class SSHClient:
    hostname: str
    username: str
    _client: paramiko.SSHClient

    @classmethod
    def connect(
        cls,
        hostname: str,
        username: str = "",
        **kwargs
    ) -> Self:
        client = paramiko.SSHClient()
        client.connect(
            hostname,
            username=username,
            **kwargs,
        )

        return cls(
            hostname=hostname,
            username=username,
            _client=client,
        )

Could you let me know which way would be cleaner, and why please?

Posted Dec 7, 2025, 12:44 PM UTC

15
Loading comments…