2
>>> 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?

Mike
  • 58,961
  • 76
  • 175
  • 221

4 Answers4

5

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
lvc
  • 34,233
  • 10
  • 73
  • 98
4

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.

Community
  • 1
  • 1
kindall
  • 178,883
  • 35
  • 278
  • 309
1

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

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.

Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176