python – 何时以及如何更改对象的__class__ attr?
作者:互联网
我希望能够做到:
>>> class a(str):
... pass
...
>>> b = a()
>>> b.__class__ = str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: only for heap types
解决方法:
我用这种方式解决了它:
>>> class C(str):
... def __getattribute__(self, name):
... if name == '__class__':
... return str
... else:
... return super(C, self).__getattribute__(name)
...
>>> c = C()
>>> c.__class__
<type 'str'>
标签:python-datamodel,python 来源: https://codeday.me/bug/20191001/1837668.html