我希望用python做个指针类,它可以被多重继承,然后拥有多重继承的第二个类的方法和代码提示,但它好像并不能调用被指向对象的魔法方法
我不希望在Pointer类里面重写所有魔法方法,不仅是因为麻烦,更是因为这样没有代码提示了
环境:
windows11 x64
python3.9
Pointer.py
class Pointer: def __new__(cls, *args, **kwargs): if cls == Pointer: return super().__new__(cls) if 'cls_' in kwargs: kwargs.pop('cls_') return Pointer(*args, **kwargs, cls_=cls) def __init__(self, value=None, *, cls_=None): self.__interior = True self.__value = value for i in dir(value): if i != '__class__': if i in cls_.__dict__: # print(i) setattr(self, i, cls_.__dict__[i]) try: setattr(self, i, getattr(value, i)) except AttributeError: pass self.__interior = False class StrPointer(Pointer, str): pass
test.py
from Pointer import StrPointer s = StrPointer('a') print(s.__str__()) print(s)
输出
a <Pointer.Pointer object at 0x0000020C8200A850>
print
用的是str
,它用的type(object).__str__(object)
。把__str__
放到self
里是没有用的。因为它用的不是object.__str__
。