在 Python2 中,如何强制 child class 方法调用 parent 方法而不明确要求最终用户包含它?
In Python2, how to force a child class method to call a parent method without explicitly requiring the end user to include it?
A parent class 我写的是使用后需要进行一些特定的内部清理。 child class 有自己的清理工作,但 parent 的清理功能必须在 运行 之后。显然,调用 super 可以解决这个问题,但我希望 child class 方面尽可能简单。
我尝试装饰 parent 方法。这没有用。
# The parent class whose inner-workings I don't expect the end user to understand
class ParentClass(object):
def __init__(self, *args, **kwargs):
self._personal_message = "Parent class says:"
self._important_message = "I'm important!"
# The method that NEEDS to be run in all instances of ParentClass and its subclasses
def _important_method(self):
print(self._important_message)
# The decorator I thought would work
def _pretty_decoration(func):
def func_wrapper(self):
func_self = func(self)
self._important_method()
return func_self
return func_wrapper
# The decorated function that will be overridden by the child class
@_pretty_decoration
def do_something(self):
print(self._personal_message)
# Make the decorator static
_pretty_decoration = staticmethod(_pretty_decoration)
# The blissfully naive Child class
class ChildClass(ParentClass):
def __init__(self, *args, **kwargs):
super(ChildClass, self).__init__(*args, **kwargs)
self._personal_message = "Child class says:"
# The overriding method
def do_something(self):
print(self._personal_message)
self.do_something_else()
def do_something_else(self):
print("I am blissfully naive.")
# The test drive
parent = ParentClass()
parent.do_something()
child = ChildClass()
child.do_something()
在这个例子中,我得到:
Parent class says:
I'm important!
Child class says:
I am blissfully naive.
而我希望得到:
Parent class says:
I'm important!
Child class says:
I am blissfully naive.
I'm important!
我应该怎么做才能达到预期的效果?
与其覆盖该方法,不如将真正的工作推迟到从 do_something
调用的回调方法。那么就没有理由覆盖do_something
,你可以直接将对_important_method
的调用放在它的主体中。
class ParentClass(object):
def __init__(self, *args, **kwargs):
self._personal_message = "Parent class says:"
self._important_message = "I'm important!"
# The method that NEEDS to be run in all instances
# of ParentClass and its subclasses
def _important_method(self):
print(self._important_message)
# This doesn't get overriden; it's a fixed entry point to do_body
def do_something(self):
self.do_body()
self._important_method()
# This shouldn't (need to) be called directly
def do_body(self):
print(self._personal_message)
class ChildClass(ParentClass):
def do_body(self):
print(self._personal_message) # or super().do_body()
self.do_something_else()
def do_something_else(self):
print("I am blissfully naive.")
那么下面的仍然有效
child = ChildClass()
child.do_something()
A parent class 我写的是使用后需要进行一些特定的内部清理。 child class 有自己的清理工作,但 parent 的清理功能必须在 运行 之后。显然,调用 super 可以解决这个问题,但我希望 child class 方面尽可能简单。
我尝试装饰 parent 方法。这没有用。
# The parent class whose inner-workings I don't expect the end user to understand
class ParentClass(object):
def __init__(self, *args, **kwargs):
self._personal_message = "Parent class says:"
self._important_message = "I'm important!"
# The method that NEEDS to be run in all instances of ParentClass and its subclasses
def _important_method(self):
print(self._important_message)
# The decorator I thought would work
def _pretty_decoration(func):
def func_wrapper(self):
func_self = func(self)
self._important_method()
return func_self
return func_wrapper
# The decorated function that will be overridden by the child class
@_pretty_decoration
def do_something(self):
print(self._personal_message)
# Make the decorator static
_pretty_decoration = staticmethod(_pretty_decoration)
# The blissfully naive Child class
class ChildClass(ParentClass):
def __init__(self, *args, **kwargs):
super(ChildClass, self).__init__(*args, **kwargs)
self._personal_message = "Child class says:"
# The overriding method
def do_something(self):
print(self._personal_message)
self.do_something_else()
def do_something_else(self):
print("I am blissfully naive.")
# The test drive
parent = ParentClass()
parent.do_something()
child = ChildClass()
child.do_something()
在这个例子中,我得到:
Parent class says:
I'm important!
Child class says:
I am blissfully naive.
而我希望得到:
Parent class says:
I'm important!
Child class says:
I am blissfully naive.
I'm important!
我应该怎么做才能达到预期的效果?
与其覆盖该方法,不如将真正的工作推迟到从 do_something
调用的回调方法。那么就没有理由覆盖do_something
,你可以直接将对_important_method
的调用放在它的主体中。
class ParentClass(object):
def __init__(self, *args, **kwargs):
self._personal_message = "Parent class says:"
self._important_message = "I'm important!"
# The method that NEEDS to be run in all instances
# of ParentClass and its subclasses
def _important_method(self):
print(self._important_message)
# This doesn't get overriden; it's a fixed entry point to do_body
def do_something(self):
self.do_body()
self._important_method()
# This shouldn't (need to) be called directly
def do_body(self):
print(self._personal_message)
class ChildClass(ParentClass):
def do_body(self):
print(self._personal_message) # or super().do_body()
self.do_something_else()
def do_something_else(self):
print("I am blissfully naive.")
那么下面的仍然有效
child = ChildClass()
child.do_something()