Py学习  »  Python

如何在python 3版本中使用super

Palla veera • 5 年前 • 1610 次点击  

我们可以在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)

 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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53769
 
1610 次点击  
文章 [ 1 ]  |  最新文章 5 年前
blhsing
Reply   •   1 楼
blhsing    6 年前

你可以打电话 super() ConnectInteractive.__init__

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