Reputation: 60743
>>> type(type)
<type 'type'>
I expect the type of type
to be a function since it's used to return the type of the argument. Does this mean a type can accept arguments? Is type
something unusual/special?
Upvotes: 2
Views: 175
Reputation: 184071
Classes are objects. All objects are instances of a class. So since a class is an object, it is an instance of some class. The class that a class is an instance of is named type
. It is the base metaclass.
Originally type()
was just a function that returned an object's class. In Python 2.2, user-defined classes and built-in types were unified, and type
became a class. For backward compatibility, it still behaves like the old type()
function when called with one argument.
Upvotes: 4
Reputation: 35059
type
is a metaclass: a class whose instances are also classes. The type of any other builtin class will also be type
- eg:
>>> type(object)
<class 'type'>
>>> type(list)
<class 'type'>
Indeed, every (new-style) class will also be an instance of type
, although if it is defined with a custom metaclass, type(my_class)
will be that metaclass. But since every metaclass is required to inherit from type
, you will have, for any class:
>>> isinstance(my_class, type)
True
Upvotes: 5
Reputation: 90859
As can be clearly seen in the documentations -
class type(object)
class type(name, bases, dict)With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing the type of an object.
type
is a class, not a function.
Upvotes: 1
Reputation: 798436
type
is Python's class ur-type. When called with a single argument it returns the type of the argument. When called with 3 arguments it returns a class whose characteristics are determined by the arguments.
Upvotes: 1