一、子继承
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}