When where and how can i change the __class__ attr of an object?

前端 未结 4 1585
-上瘾入骨i
-上瘾入骨i 2020-12-16 13:53

I\'d like to be able to do:

>>> class a(str):
...     pass
...
>>> b = a()
>>> b.__class__ = str
Traceback (most recent call last)         


        
4条回答
  •  甜味超标
    2020-12-16 14:05

    I've solved it in this way:

    >>> class C(str):
    ...     def __getattribute__(self, name):
    ...         if name == '__class__':
    ...             return str
    ...         else:
    ...             return super(C, self).__getattribute__(name)
    ...         
    >>> c = C()
    >>> c.__class__
    
    

提交回复
热议问题