Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Learn Python Identity Operator and Difference Between “==” and “IS” Operator - DesignLinux

May 29 2020
designlinux 0 Comments

This article is mainly curated to explain an important operator in python (“IDENTITY OPERATOR”) and how an identity operator differs (is, is not) from comparison operator (==).

IDENTITY OPERATOR

Identity operator (“is” and “is not”) is used to compare the object’s memory location. When an object is created in memory a unique memory address is allocated to that object.

  • ‘==’ compares if both the object values are identical or not.
  • ‘is’ compares if both the object belongs to the same memory location.

Create three string objects Name, Name1, and Name2. String object Name and Name2 will hold the same value and Name1 will hold different values.

When we create these objects, what happens behind the scene is, that object will be created in memory and will be available during the lifetime of the program.

Now you can use a comparison operator “==” to check if both the object values are the same. The output of the comparison operator will be a Boolean (True or False) value.

Create and Compare Objects

Create and Compare Objects

Now that you have compared two values to determine for equality, let us take a look at how the identity operator works.

The built-in Id() a function is used to get the “identity” of an object. An integer that will be unique and constant for the object during its lifetime.

To make it simple think this as unique government ID or Emp ID assigned to you, likewise a unique integer value is assigned for each object.

Unique Integer Value

Unique Integer Value

Now you can compare 2 object references using “is” operator.

Identity Operator

Identity Operator

When I compare Name and Name1 or Name2 using the identity operator what it does at the backend is it simply runs “id(Name) == id(Name2)”. Since id(Name) and id(Name2) both share the same memory location, it returns True.

Now here comes the interesting part. Look at our previous example where both Name and Name1 have identical values and returns the same integer value when we run id() function. Why do you think “Name_new” and “Name_le” object is not identical even though they share the same values from the below screenshot?

Integer Values

Integer Values

This is because of the python design implementation. When you create an integer object in range (-5,256) and string objects greater than or equal to 20 chars, instead of creating different objects at memory for the same value these objects act as a pointer to already created objects.

Below pictorial representation will give you a clear idea of what we have seen so far in this article.

Diagrammatic Representation

Diagrammatic Representation

Summary

In this article, we have seen what is an identity operator. How comparison operator and identity operator is used, design implementation on how an object is created in memory.

Sharing is Caring…
Share on FacebookShare on TwitterShare on LinkedinShare on Reddit

Related

Tags: python, Python Programming, Python Tips

How to Install TeamViewer on CentOS 8

Prev Post

How to Reset Forgotten Root Password in RHEL 8

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 63
  • 605,813

DesignLinux.com © All rights reserved

Go to mobile version