DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Python 101: Equality vs. Identity

Python 101: Equality vs. Identity

Even experienced programmers find the difference subtle enough that they will introduce logic errors in their code due to a misunderstanding between the two.

Mike Driscoll user avatar by
Mike Driscoll
·
Jun. 21, 22 · Opinion
Like (0)
Save
Tweet
Share
11.81K Views

Join the DZone community and get the full member experience.

Join For Free

People who are new to the Python programming language can get a bit confused about the difference between == (equality) and Python’s keyword is (identity). I have even seen experienced programmers who will find the difference subtle enough that they will introduce logic errors in their code due to a misunderstanding between the two. In this article, we will look at this interesting topic.

Equality in Python

The Equality operator (==) is a comparison operator in Python that compare values of both the operands and checks for value equality. Whereas the ‘is’ operator is the membership operator that checks whether both the operands refer to the same object or not (present in the same memory location).

Many programming languages have the concept of equality and several use the double equals sign (==) to designate this concept. Let's take a look at equality in action:

>>> num = 1 >>> num_two = num >>> num == num_two True

Here, we create a variable that we call num and assign it to the integer 1. Next, we create a second variable called num_two and assign it to the value of num. Finally, we ask Python if num and num_two are equal. In this case, Python tells us that this expression is True.

Another way to think of equality is that we are asking Python if two variables contain the same thing. In the example above, they both contain the integer 1. Let’s see what happens when we create two lists with the same values:

>>> list_one = [1, 2, 3] >>> list_two = [1, 2, 3] >>> list_one == list_two True

This worked out the way we expected.

Now let’s see what happens if we ask Python about their identity:

>>> num is num_two True >>> list_one is list_two False

What happened here? The first example returned True, but the second returned False! We will look into that in the next section.

Identity in Python

When you ask Python about whether one object is the same as another object, you are asking if they have the same identity. Are they actually the same object? In the case of num and num_two, the answer is yes. Python provides an easy way to prove it via it’s built-in id() function:

>>> id(num) 10914368 >>> id(num_two) 10914368

The reason that these two variables share the same identity is because we told Python they should back when we assigned num to num_two (i.e. num_two = num). If you come from C or C++, you can think of the identity as a pointer where num and num_two are both pointing to the same place in memory. If you use Python’s id() function on the two list objects, you will quickly see that they have different identities:

>>> id(list_one) 140401050827592 >>> id(list_two) 140401050827976

Thus, when you ask Python the question list_one is list_two, you receive False. Note that you can also ask Python if one object is not another object:

>>> list_one = [1, 2, 3] >>> list_two = [1, 2, 3] >>> list_one is not list_two True

Let’s take a moment to find out what happens when you mix equality and identity up.

Mixing It Up

I know when I was starting out as a Python programmer, these sorts of things would lead to silly mistakes. The reason is that I would see recommended statements like this one:

if obj is None: # do something call_function()

So, I would assume naively that you could do something like this:

>>> def func(): return [1, 2, 3]   >>> list_one = [1, 2, 3] >>> list_two = func() >>> list_one is list_two False

Of course, that doesn’t work as I now have two different objects with different identities. What I wanted to do here was this:

>>> list_one == list_two True

Another issue that is tangential to this one is when you create two variables that point to the same object, but you think you can work on them independently of each other:

>>> list_one = list_two = [1, 2, 3] >>> list_one == list_two True >>> list_one is list_two True >>> list_two.append(5) >>> list_one [1, 2, 3, 5]

In this example, I created two variables that both pointed at one object. Then I tried adding an element to just list_two. What a lot of beginners don’t realize is that they just added that element to list_one as well. The reason for this is that both list_one and list_two are pointing at the exact same object. This is proven when we asked Python is list_one is list_two and it returned True.

Wrapping Up

Hopefully, by now you understand the differences between equality (==) and identity (is) in Python. Equality is basically just asking if the contents of the two object are the same and in the case of lists, it needs to be in the same order as well. Identity in Python refers to the object you are referring to. In Python, the identity of an object is a unique, constant integer (or long integer) that exists for the length of the object’s life.

Python (language) Object (computer science)

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Debezium vs DBConvert Streams: Which Offers Superior Performance in Data Streaming?
  • Getting a Private SSL Certificate Free of Cost
  • Low-Code Development: The Future of Software Development
  • How To Use Linux Containers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: