Is there a value in Python for which isststance (value, object) is not True?

I understand that since type / class unification, each value has a type that comes from object . However, I can not find absolute confirmation of this in the documents. Although it is reasonable that isinstance(anything, object) should always be True , I could also imagine that there are outdated edge cases in the Python 2 codebase. Does anyone know an example where isinstance(value, object) not True ?

Context: as part of the type hierarchy I am developing, there is an all-inclusive Alpha type for which I want isinstance(obj, Alpha) always return True . I think in Python 2.6+ ABCMeta.register(object) should do the trick, but I want to be sure.

EDIT: for the sake of posterity, ABCMeta.register(object) will not work (try). Ethan Furman provides an alternative solution for this case in his answer below.

+6
source share
2 answers

You can create classes in non-Python code ( C , for example) that are not inferred from object .

You can achieve what you want by adding __subclasshook__ to your Alpha :

 --> import abc --> class Test(object): ... __metaclass__ = abc.ABCMeta ... @classmethod ... def __subclasshook__(cls, C): ... return True ... --> isinstance(dict(), Test) True --> isinstance(42, Test) True --> isinstance(0.59, Test) True --> class old_style: ... pass ... --> isinstance(old_style(), Test) True 
+1
source

In 2.x, user classes (and several stdlib classes) are not inferred from the object by default. This is fixed at 3.x.

0
source

Source: https://habr.com/ru/post/910369/


All Articles