在 Python 中进行微小更改来覆盖方法的正确方法
Proper way to override methods with minor changes in Python
假设我有一个 class
class Base(object):
def my_method(self, input):
print input #suppose this is many lines
print "mymethod" #so is this
和一个 subclass 有一个方法做几乎相同的事情,除了在方法的 middle 中的额外操作,例如
class Sub(Base):
def mymethod(self, input): #how do I properly define this?
print input
print "some other stuff" #additional operation in the middle
print "mymethod"
覆盖mymethod
的正确方法是什么?
- 我是否复制并粘贴了大部分
Base.mymethod()
? (可能不是——这绝对违反了 DRY)。
- 我是否将
Base.mymethod()
定义为具有附加操作的条件语句,在子 class 情况下仅 returns 为真? (可能不是 - 这没有意义,因为基础 class 应该是独立的,这似乎是灾难的根源)
- 我能以某种方式使用
super()
吗? (好像不是——Sub
的追加操作是在方法的中间,不是开头也不是结尾)
这取决于物品所属的位置。通常如果你最终想在基础方法的操作之间插入东西,这意味着该方法实际上应该分成几个方法。
例如:
class Base(object):
def my_method(self, input):
print input #suppose this is many lines
print "mymethod" #so is this
可能会变成:
class Base(object):
def my_method(self, input):
self.do_first_thing(input)
self.do_second_thing("mymethod")
def do_first_thing(self, input):
print(input)
def do_second_thing(self, data):
print(data)
这让子类可以重新定义整个过程,而不必重新实现每个步骤。这个概念类似于 template method,但倒退了。
(通常模板方法模式的重点是让子类重新定义步骤,这里我们使用相同的结构让子类重新定义模板本身)。
对于这样一个简单的例子,我很可能会复制这三个小行,即使创建重复。尽量避免 over-engineering.
在my_method()
其实比较复杂的情况下,你可以把你的函数分成三步,让child类重载他们想要的部分。
class Base(object):
def my_method(self, input):
self._preprocess(input)
self._process()
self._postprocess()
def _preprocess(self, input):
print(input)
def _process(self):
pass
def _postprocess(self):
print("mymethod")
class Sub(Base):
def _process(self):
print("some other stuff")
当然你应该使用更有意义的方法名称。
假设我有一个 class
class Base(object):
def my_method(self, input):
print input #suppose this is many lines
print "mymethod" #so is this
和一个 subclass 有一个方法做几乎相同的事情,除了在方法的 middle 中的额外操作,例如
class Sub(Base):
def mymethod(self, input): #how do I properly define this?
print input
print "some other stuff" #additional operation in the middle
print "mymethod"
覆盖mymethod
的正确方法是什么?
- 我是否复制并粘贴了大部分
Base.mymethod()
? (可能不是——这绝对违反了 DRY)。 - 我是否将
Base.mymethod()
定义为具有附加操作的条件语句,在子 class 情况下仅 returns 为真? (可能不是 - 这没有意义,因为基础 class 应该是独立的,这似乎是灾难的根源) - 我能以某种方式使用
super()
吗? (好像不是——Sub
的追加操作是在方法的中间,不是开头也不是结尾)
这取决于物品所属的位置。通常如果你最终想在基础方法的操作之间插入东西,这意味着该方法实际上应该分成几个方法。
例如:
class Base(object):
def my_method(self, input):
print input #suppose this is many lines
print "mymethod" #so is this
可能会变成:
class Base(object):
def my_method(self, input):
self.do_first_thing(input)
self.do_second_thing("mymethod")
def do_first_thing(self, input):
print(input)
def do_second_thing(self, data):
print(data)
这让子类可以重新定义整个过程,而不必重新实现每个步骤。这个概念类似于 template method,但倒退了。
(通常模板方法模式的重点是让子类重新定义步骤,这里我们使用相同的结构让子类重新定义模板本身)。
对于这样一个简单的例子,我很可能会复制这三个小行,即使创建重复。尽量避免 over-engineering.
在my_method()
其实比较复杂的情况下,你可以把你的函数分成三步,让child类重载他们想要的部分。
class Base(object):
def my_method(self, input):
self._preprocess(input)
self._process()
self._postprocess()
def _preprocess(self, input):
print(input)
def _process(self):
pass
def _postprocess(self):
print("mymethod")
class Sub(Base):
def _process(self):
print("some other stuff")
当然你应该使用更有意义的方法名称。