Python3:无法让子类正确地继承父类方法

时间:2017-10-24 20:38:17

标签: python-3.x class inheritance

这感觉就像一个愚蠢的虫子,我要自己开始,但我觉得我已经尝试了一切。

基本上,当我尝试从孩子访问父方法时,它说我错过了目标参数:

Traceback (most recent call last):
  File "remote_submission_rand.py", line 113, in <module>
    XYZ.rsyncFile(source, outfiles)
TypeError: rsyncFile() missing 1 required positional argument: 'destination'

这显然听起来我在组织继承时对自己做错了但是我已经玩了几个小时并且无法获得有效的东西。任何建议都会被贬低?

请参阅下面的父母和子女课程:

儿童班:

from abc import ABCMeta, abstractmethod
import subprocess
import sys
class Connection(metaclass=ABCMeta):
        """This is an abstract class that all cluster classes inherit from.""" 
        def __init__(self, cluster_user_name, ssh_config_alias, path_to_key):
                """In order to initiate this class the user must have their ssh config file set up to have their cluster connection as an alias."""
                self.user_name = cluster_user_name 
                self.ssh_config_alias = ssh_config_alias
                self.path_to_key = path_to_key
                # add the ssh key so that if there's a password then the user can enter it now
                ssh_add_cmd = "eval `ssh-agent -s`;ssh-add " + self.path_to_key
                subprocess.check_output(ssh_add_cmd, shell=True)
                self.job_numbers_to_wait_for = []

        # instance methods
        def rsyncFile(self, source, destination, rsync_flags = "-aP"):
                rsync_cmd = ["rsync", rsync_flags, source, self.ssh_config_alias + ":" + destination]
                exit_code = self.sendCommand(rsync_cmd)                                                                                                                  
                return exit_code

家长班:

from base_connection import Connection
import subprocess
class XYZ(Connection):
        def __init__(self, cluster_user_name, ssh_config_alias, path_to_key):
                Connection.__init__(self, cluster_user_name, ssh_config_alias, path_to_key)

        #instance methhods
        def rsyncFile(self, source, destination, rsync_flags = "-aP"):
                super(XYZ, self).rsyncFile(source, destination, rsync_flags)

                return

使用课程:

from connections import XYZ

outfiles = '/path/to/outfiles'
source_path = '/path/a_file.list'

XYZ.rsyncFile(source_path, outfiles)

此外: 我也尝试过在XYZ类中没有rsyncFile函数,并且得到了完全相同的错误。

1 个答案:

答案 0 :(得分:0)

很抱歉,但我意识到我做了一些非常愚蠢的事情(一定需要睡一觉!)。

发生了什么事我忘了实际创建一个类的实例!我也忘了在几个功能中宣布自我,导致之前的一些调用工作,这反过来又导致我误入歧途。无论如何,解决方案不是:

from connections import XYZ

outfiles = '/path/to/outfiles'
source_path = '/path/a_file.list'
XYZ_inputs = stuff

XYZ_conn = XYZ(XYZ_inputs)
XYZ_conn.rsyncFile(source_path, outfiles)

我们应该:

{{1}}