【问题标题】:Python3: Can't get child class to inherit parent class method properlyPython3:无法让子类正确继承父类方法
【发布时间】:2018-04-05 18:52:38
【问题描述】:

这感觉就像一个愚蠢的错误,我要把自己踢倒,但我觉得我已经尝试了一切。

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

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'

这显然听起来像是我在组织继承时对 self 做错了,但我已经玩了几个小时并且无法得到有效的东西。有什么建议会很感激吗?

请参阅下面的父类和子类:

儿童班:

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 函数,得到了完全相同的错误。

【问题讨论】:

    标签: python-3.x class inheritance


    【解决方案1】:

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

    发生的事情是我忘了实际创建类的实例!我还忘记在几个函数中声明 self ,这导致以前的一些调用正常工作,这反过来又导致我误入歧途。无论如何,解决方案是:

    from connections import XYZ
    
    outfiles = '/path/to/outfiles'
    source_path = '/path/a_file.list'
    
    XYZ.rsyncFile(source_path, outfiles)
    

    我们应该有:

    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)
    

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2020-11-13
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      相关资源
      最近更新 更多