一、子继承

class A(object):
    def __new__(cls, *args, **kwargs):
        # 1. 获取 继承 此类的函数中的所有方法
        now_dict = cls.__dict__.copy()

        # 2. 删除其他
        now_dict.pop("__module__", None)
        now_dict.pop("__doc__", None)

        # 3. 赋值变量
        cls.now_dict = now_dict

        # 4. 返回对象
        return super().__new__(cls, *args, **kwargs)

    def a(self):
        print(123)

    def b(self):
        print(456)


class B(A):
    name = 123
    act = 456


a = B()
print(a.now_dict)


# 结果
{'name': 123, 'act': 456}

 二、孙子继承

class A(object):
    def __new__(cls, *args, **kwargs):
        # 1. 获取 继承 此类的函数中的所有方法
        now_dict = cls.__dict__.copy()

        # 2. 删除其他
        now_dict.pop("__module__", None)
        now_dict.pop("__doc__", None)

        # 3. 赋值变量
        cls.now_dict = now_dict

        # 4. 返回对象
        return super().__new__(cls, *args, **kwargs)

    def a(self):
        print(123)

    def b(self):
        print(456)


class B(A):
    name = 123
    act = 456


class C(B):
    age = 22


a = C()
print(a.now_dict)

# 结果
{'age': 22}

相关文章:

  • 2021-07-03
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2021-06-30
  • 2022-01-14
  • 2021-09-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-02-08
相关资源
相似解决方案