首页 > 解决方案 > 创建一个在调用另一个类函数时调用的类函数 Python

问题描述

我想要一个包含一个函数的类,每当调用另一个类函数时都会调用该函数。棘手的部分是我希望它适用于派生类。

比方说:

def class A:
  def __init__(self):
    pass
  def b(self):
    print("Hello")

def class B(A):
  def __special_method__(self):
    print("Before Hello")

调用 object.b() 会打印

Before Hello
Hello

这甚至可能吗?

标签: python

解决方案


推荐阅读