"Equality" ... lovely subject. We're all equal in the eyes of many laws these days - man or workman, able bodied or not, young or old, left or right handed, any colour or religion. But just because we're equal doesn't mean we're the same.
In Python, the
== operator returns
True if the two parameters passed to it have an equal value, whether or not the two objects are the same object. Whereas the
is operator only returns
True if the parameters and one and the same object (i.e. if the
id function returns the same value on both of the). Example program
[here].
>>> a = [2,3,4]
>>> b = [2,3,4]
>>> a == b
True
>>> a is b
False
>>> c = a
>>> a == c
True
>>> a is c
True
>>>
Note that many simple immutable objects (such as integers) are only held once on the heap, and you may be surprised to get back a
True response from
is when you compare two variables derived in different ways. That's not the case for mutable objects such as types you're likely to create for yourself, or for lists and dicts.
You can override the
== operator in your own classes by specifying a
__eq__ method.
Want to know more? Read
[[about our Python courses]] where we cover this and much more!
(written 2016-10-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y199 - Python - Fast Start [2017] Python - a truly dynamic language - (2009-01-30)
[3519] Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13)
[4707] Some gems from an introduction to Python - (2016-10-29)
[4709] Some gems from Intermediate Python - (2016-10-30)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
Y103 - Python - Conditionals and Loops [299] What - no switch or case statement? - (2005-05-03)
[353] Wimbledon Neck - (2005-06-20)
[657] The ternary operator in Python - (2006-03-25)
[668] Python - block insets help with documentation - (2006-04-04)
[788] New - Conditional expressions in Python 2.5 - (2006-07-01)
[835] Python - when to use the in operator - (2006-08-16)
[909] Python is like a narrowboat - (2006-10-30)
[1201] No switch in Python - (2007-05-23)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3762] Learning to program - the if statement. Python. - (2012-06-12)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4092] Identity in Python - (2013-05-17)
[4210] If elif elif elif - multiway selection in Python - (2013-11-16)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
[4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
[4723] Conditional operators in Python - (2016-11-05)
Some other Articles
with in Python - examples of use, and of defining your own contextProfiling your Python programWell House Manor - Still five out of five!The technical article feed continues - personal updates more proactive on Facebook now!Equality (in Python)Convering from Python 2 to Python 3 - an update, and the 2to3 utilitySearching a Json or XML structure for a specific key / value pair in PythonScons - a build system in Python - building hello world