使用父方法而不是重写方法的Python库
我用使用父方法而不是重写方法的Python库,python,inheritance,Python,Inheritance,我用覆盖类扩展了父类类。我已经重写了method()方法来修复Parent类中出现的错误。我修复了上面提到的bug,并且这个修复已经在Override类中进行了测试 class Parent(): def method(self, param): # Bugged do_stuff() class Override(Parent): def method(self, param): # Fixed (tested)
覆盖
类扩展了父类
类。我已经重写了method()
方法来修复Parent
类中出现的错误。我修复了上面提到的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
类以查看以前的bug是否已修复,我发现它没有修复,并且回溯不会通过覆盖
类
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()
方法。关于如何解决这个问题或者这个问题是从哪里来的,有什么线索吗
我是一个初学者,并没有遇到很多继承,所以,请原谅我的无知和我缺乏经验
编辑
在外部
中失败的测试:
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"})
重写方法:
读取第573行中的字符串()
父方法:
从configparser
()
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
您的代码不是一个完整的工作示例,但它应该按照您的建议执行
下面是一个你可以运行来证明这一点的例子:
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)。您需要显示实际代码和错误。您发布的代码不会显示您声称的行为。请发布一个MCVE。这应该是一个评论,而不是答案。是的,这是我期望的行为,但在我的情况下,它似乎使用了父方法,我不明白为什么。