Can a non-object exist in Python?

As you know, all this is an object in Python. I am wondering if it is possible to create an “object” xto isinstance(x, object)return False. I suspect this is possible with enough abuse of the CPython API, although achieving this with pure Python would be even more interesting.

Initially, I thought that old-style classes would return False, since the hierarchy of objects might not be fully applicable, but it seems to be isinstance(x, object)valid Truefor instances of old-style classes.

Although this is mostly of theoretical interest, it can be interesting (or dangerous) if Python allows you to create a new hierarchy of objects that is disconnected from the base type object.

+4
source share
2 answers

In the docs, chapter 3 (Data Model): "All data in a Python program is represented by objects." It seems clear enough. You say that the alternative may be interesting, but there are already many interesting things that are not Python.

+1
source

Of course, you can do everything through the C API (in particular, through a great module forbiddenfruit)

>>> from forbiddenfruit import curse
>>> class C: pass
... 
>>> curse(type, "__instancecheck__", lambda cls, obj: type(obj) != C)
>>> isinstance(C(), object)
False
>>> isinstance(C(), C)
True
+1
source

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


All Articles