如何使用超在Python 3版本

时间:2019-02-02 15:26:44

标签: python python-3.x

我们可以像下面这样在python 3中实现super()

class A(object):
    def __init__(self, Name, Age):
        self.Name = Name
        self.Age = Age

class B(A):
    def __init__(self, Name, Age):
        super().__init__(Name, Age)

我有下面的情况下是有可能实现超级()类ConnectInteractive

 class Connect(object):
    def __init__(self,ip, username, password):
         self.client = paramiko.SSHClient()
         self.client.set_missing_host_key_policy(paramiko.AutoaddPolicy())
         self.client.connect(ip,
         username=username,
         pasword=password)

class ConnectInteractive(Connect):
    def __init__(self,ip, username, password):
         self.client = paramiko.SSHClient()
         self.client.set_missing_host_key_policy(paramiko.AutoaddPolicy())
         self.client.connect(ip,
         username=username,
         pasword=password)
         self.client = self.client.invoke_shell()

因为以下代码是重复的:

  self.client = paramiko.SSHClient()
  self.client.set_missing_host_key_policy(paramiko.AutoaddPolicy())
  self.client.connect(ip,
  username=username,
  pasword=password)

1 个答案:

答案 0 :(得分:1)

您可以像下面这样从super()方法中调用ConnectInteractive.__init__

class ConnectInteractive(Connect):
    def __init__(self, ip, username, password):
         super().__init__(ip, username, password)
         self.client = self.client.invoke_shell()