Search code examples
pythonbuilt-in-types

Are type on python's builtin unique


Can I assume that that type objects of python's built ins are always the same? Example:

type([]) == type([1,2,3,4]) == type([{1:1}, True])
type(5) == type(0) == type(2**18) == type(-951)

If so, can I use "is" instead of "==" when comparing type objects? The question is referring to all builtin types.

Thanks in advance!


Solution

  • In Python 2: Generally, yes. However, integers are somewhat of an exception: there's two different types of integers: ints and longs. The latter is capable of holding integers up to an arbitrary size, whereas the former can only hold integers between -2**31 to 2**31-1.

    So, for example, type(3) is type(2**50) will return False. However, it would return False just the same if you used == instead of is.