What is the purpose of type() and the types module and what is a type?

Russel Walker russ.pobox at gmail.com
Thu Jun 27 04:34:34 EDT 2013


The type() builtin according to python docs, returns a "type object".
http://docs.python.org/2/library/types.html

And in this module is bunch of what I assume are "type objects". Is this correct?
http://docs.python.org/2/library/functions.html#type

And type(), aside from being used in as an alternative to a class statement to create a new type, really just returns the object class, doesn't it?


>>> import types
>>> a = type(1)
>>> b = (1).__class__
>>> c = int
>>> d = types.IntType
>>> a is b is c is d
True
>>> 


If type() didn't exist would it be much more of a matter than the following?:


def type(x): 
    return x.__class__


What is the purpose of type()?
What exactly is a "type object"? Is it a "class"?
What is the purpose of the types module?

I understand the purpose of isinstance and why it's recommended over something like (type(1) is int). Because isinstance will also return True if the object is an instance of a subclass.


>>> class xint(int):
	def __init__(self):
		pass


>>> x = xint()
>>> type(x) is int
False
>>> isinstance(x, int)
True
>>> 



More information about the Python-list mailing list