使用父方法而不是重写方法的Python库

2024-06-02 04:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Override类扩展了Parent类。我已经重写了method()方法来修复Parent类中出现的错误。我修复了上述bug,并且这个修复已经在Override类中进行了测试。你知道吗

我通过External类使用Override类。通过测试External类以查看前面的bug是否已修复,我发现它没有修复,并且回溯不会通过Override类。你知道吗

class Parent():

    def method(self, param):
        # Bugged
        do_stuff()

class Override(Parent):

    def method(self, param):
        # Fixed (tested)
        param = fix_param(param)
        super(Parent, self).method(param)

class External():

    def processing():
        # Same bug as in `Parent`
        param = get_param()
        obj = Override()
        obj.method(param)

在我看来,External类使用了Parent.method()方法而不是Override.method()方法。关于如何解决这个问题或者这个问题的来源有什么线索吗?你知道吗

我是一个初学者,没有遇到过很多,所以,请原谅我的无知和我的经验不足。你知道吗

编辑

External中失败的测试:

import os
import collections
import envtpl
from acquisition.configargparse_confparser import StepConfigFileParser
from configparser_extended import ExtendedConfigParser
from unittest import TestCase
from acquisition.utils import set_custom_environment


class ConfigFileParserTestCase(TestCase):

def test_parse_extended(self):
    # x = StepConfigFileParser("test_plugin_name", "test")
    plugin_name = "test_plugin_name"
    step_name = "test"
    set_custom_environment(plugin_name, step_name)
    config = os.environ.get('MFCONFIG', 'GENERIC')
    filename = os.path.dirname(os.path.realpath(__file__)) + "/test.ini"
    with open(filename, 'r') as stream:
        config_parser = ExtendedConfigParser(
            config=config, inheritance='im', interpolation=None)
        content = stream.read()
        config_parser.read_string(content)  # Fails here
        section = "step_%s" % step_name
        res = collections.OrderedDict()
        for key in config_parser.options(section):
            if not key.startswith('arg_'):
                continue
            res[key.replace('arg_', '', 1)] = envtpl.render_string(
                config_parser.get(section, key))
    self.assertEqual(res, {"venom": "snake", "revolver": "ocelot"})

重写的方法: ^https://github.com/thefab/configparser_extended/blob/master/configparser_extended/ecp.py行573中的{}

父方法: read_string()来自configparserhttps://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_string

test.ini

[step_test]
arg_venom=snake
arg_revolver=ocelot
liquid=snake

错误:

ERROR: test_parse_extended (tests.test_confparser.ConfigFileParserTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/florian/metwork/mfdata/src/acquisition/tests/test_confparser.py", line 39, in test_parse_extended
    config_parser.read_string(content)
  File "/opt/metwork-mfext/opt/python2/lib/python2.7/site-packages/backports/configparser/__init__.py", line 728, in read_string
    sfile = io.StringIO(string)
TypeError: initial_value must be unicode or None, not str

Tags: 方法nametestimportselfconfigextendedread
1条回答
网友
1楼 · 发布于 2024-06-02 04:25:32

您的代码不是一个完整的工作示例,但它应该按照您的建议执行。你知道吗

下面是一个可以用来证明这一点的例子:

class Parent():
    def method(self):
        print('parent')

class Child(Parent):    
    def method(self):
        print('child')

class Other():
    def call_method(self):
        c = Child()
        c.method()

o = Other()
o.call_method()

打印“child”,证明子类重写了方法(self)。你知道吗

相关问题 更多 >