Object Identity and Type

The built-in function id() returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory, although this is specific to the implementation. The is operator compares the identity of two objects. The built-in function type() returns the type of an object. For example:

# Compare two objects 
def compare(a,b): 
    print 'The identity of a is ', id(a) 
    print 'The identity of b is ', id(b) 
    if a is b: 
        print 'a and b are the same object' 
    if a == b: 
        print 'a and b have the same value' 
    if type(a) is type(b): 
        print 'a and b have the same type' 

The type of an object is itself an object. This type object is uniquely defined and is always the same for all instances of a given type. ...

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.