6.9. Determining Object Identity
It can be very useful to quickly determine if two objects are the same object. There are a number of ways to determine this.
First, Python provides the operator is for this purpose. The expression x is y returns 1 (i.e., true) if x and y are the same object and 0 (false) otherwise.
CD-ROM reference=6037.txt >>> x = [1,2,3] >>> y = x >>> print x [1, 2, 3] >>> print y [1, 2, 3] >>> x is y 1 >>> z = [1,2,3] >>> x is z 0
An alternative method of checking for sameness is to use the built-in id function. Given a variable name, the id function returns the memory address of the object referenced by the variable. If two objects have the same address in memory, they are the same object.
CD-ROM reference=6038.txt >>> ...
Get XML Processing with Python 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.