问题
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