How to fake type() response on Python class

你说的曾经没有我的故事 提交于 2021-02-11 13:46:32

问题


While it has been previously answered how to fake isinstance, in this case I'm trying to fake the response given by type() while reviewing the TypeChanger class. For context Im just trying to understand if is doable or not and why for Python 3.X

This is my current test setup:

def test():
    class TypeChanger():
        @classmethod
        def __instancecheck__(cls, instance):
            if instance is list:
                return True
            else:
                return False
        @property
        def __class__(self):
            return list
        def extra_functionality(self):
            return "Extra functionality"
    a = TypeChanger()
    return a

Test:

trial = test()
assert isinstance(trial, list) # TRUE
assert isinstance(list, trial) # TRUE
assert type(trial)==type(list) # Assert error

For extra context my goal is to maintain the class TypeChanger functionality while faking the type() response

来源:https://stackoverflow.com/questions/60123700/how-to-fake-type-response-on-python-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!