2

Are there any built-in Python classes that are not instances of type metaclass?

>>> type(list)
<type 'type'>
>>> type(dict)
<type 'type'>
>>> type(some_builtin_python_class)
<type 'some_type_other_than_type'>

1 Answer 1

4

Depends what you mean by "built-in". If you mean any of the C-implemented (in CPython) classes exposed as builtins (and in builtin), then no, none of them have a different metaclass.*

But there are definitely classes in the stdlib that do. For example:

>>> type(collections.abc.Iterable)
abc.ABCMeta

To clarify: Other metaclasses are of course subclasses of type, so, by the normal inheritance rules, it's still true that isinstance(Iterable, type)—but it's not true that type(Iterable) == type. Which is what you were asking for in your question—a type for which type(T) returns <type 'some_type_other_than_type'>.


* Not in 3.x, at any rate. Things were different in 2.x, where there were classic classes, and, once upon a time, "fake classes" that were actually functions that looked like type instances but weren't, and returned instances of a hidden type instance when called.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I mean builtin module in python. Anyway, you are right, there are no such classes in this module. I've just checked it
@IvanKalita: Much cleaner than my own check. I didn't use inspect because I wanted to run it against 2.4, but then I couldn't find my working 2.4 installation, so it was all a waste… :)
Although even collections.abc.Iterable is an instance of type. isinstance(collections.abc.Iterable, type) is True.
@OlivierMelançon Sure—metaclasses are subclasses of type, so classes with custom metaclasses are instances of type. But that's just normal inheritance; I assume he meant type(x) == type, not instance(x, type).
This answer is plain incorrect, as other metaclasses in the stdlib are also descendant of type: type(Iterable).__mro__ yields (abc.ABCMeta, type, object)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.