面试题答案
一键面试父类方法重写的底层原理
在Python中,元类是创建类的类。当使用元类创建类时,元类的__new__
或__init__
方法会被调用。在创建子类时,如果子类要重写父类的方法,Python会在类的命名空间中查找该方法。如果找到了,则使用子类中的方法定义,而不是父类的。这是因为Python的方法查找顺序是从子类开始,向上查找父类。
代码实现
class Base:
def original_method(self):
print("This is the original method in Base class.")
class Meta(type):
def __new__(mcs, name, bases, namespace):
if name != 'Base':
if 'original_method' in namespace:
original_method = namespace['original_method']
def wrapper(self, *args, **kwargs):
print("Before calling the overridden method.")
result = original_method(self, *args, **kwargs)
print("After calling the overridden method.")
return result
namespace['original_method'] = wrapper
return super().__new__(mcs, name, bases, namespace)
class Subclass(Base, metaclass=Meta):
def original_method(self):
print("This is the overridden method in Subclass.")
sub = Subclass()
sub.original_method()
可能遇到的问题及解决方案
- 方法调用链混乱:如果重写方法时没有正确调用父类方法,可能导致功能缺失。解决方案是在子类方法中显式调用父类方法,例如
super().original_method()
。 - 元类干扰:元类对类创建过程的干预可能导致难以调试。解决方案是在元类中添加详细的日志记录,以便跟踪类创建过程中的每一步。
- 多重继承冲突:如果子类继承自多个父类,且这些父类中有同名方法,可能导致方法解析顺序(MRO)问题。解决方案是使用
super()
函数按照MRO正确调用父类方法,并且明确了解MRO的计算规则。