Problem: IPython's autoreload
and superreload
may reload modules such that obj=Obj()
-> edit file & save -> isinstance(obj, Obj) == False
. Sometimes even imp.reload
of all involved modules doesn't absolve it.
Goal: a method with a 'fallback' on looser criterion that are as close to isinstance
as possible.
Attempt:
def isinstance_ipython(obj, ref_class):
def _class_name(obj):
name = getattr(obj, '__qualname__', getattr(obj, '__name__', ''))
return (getattr(obj, '__module__', '') + '.' + name).lstrip('.')
return (isinstance(obj, ref_class) or
_class_name(type(obj)) == _class_name(ref_class))
obj
is always instance of a class, and ref_class
is always a Python 3.x, non-builtin class. __qualname__
and __module__
are included to prefer longest possible name, but fallback upon shorter names via empty strings.
Any improvements to reliability of this method (i.e. reduced false positives/negatives)? Not interested in speed or readability, but they're welcome as bonus.