Python Forum
Shared reference and equality
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shared reference and equality
#1
Hi everybody,
I am starting to learn Python in these days: I came across the topic of shared references and equality, and I thought I had understood its basics. However, I came across an example where things were not so clear to me.
Here it is:

a = 'a'
b = 'b'
ab = 'ab'
a is 'a'     # returns True
b is 'b'     # returns True
ab is 'ab'   # returns False
Why does the 3rd statement return false? Is it linked to the lenght of the string 'ab'? Thanks for helping
Reply
#2
Printing the id will show if you are referencing the same block of memory (is)
print(id("ab"))
## 139660486771968

x="a"
x += "b"
print(x)
## "ab"
print(id(x))
## 139660486772024  different memory location 
Reply
#3
Small integers - -5 to 255, ASCII letters, Booleans and None have dedicated constant objects associated with them. Still, the only case when is operator is recommended is a test for None value
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
Like @volcano63 said, is tests if two variables point to the same object.

To check if values are the same use ==. In the example below mylist1 and mylist2 point to the same object. mylist3 is a copy of mylist1.
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'a'
>>> b = 'b'
>>> ab = 'ab'
>>> a == 'a'
True
>>> b == 'b'
True
>>> ab == 'ab'
True

>>> mylist1 = [1, 2, 3]
>>> mylist2 = mylist1
>>> mylist1 == mylist2
True
>>> mylist1 is mylist2
True

>>> mylist3 = list(mylist1)
>>> mylist3 == mylist1
True
>>> mylist3 is mylist1
False
For other methods to create a copy see https://stackoverflow.com/questions/2612...opy-a-list
Other methods include:
import copy
 
b = a[:]
b = list(a)
b = copy.copy(a)
b = copy.deepcopy(a)   
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,557 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Shared reference of variables... Denial 1 1,415 Aug-29-2020, 01:52 PM
Last Post: snippsat
  How to install and use a shared libary, via a .dll? ninjaisfast 0 1,298 Jul-09-2020, 03:23 PM
Last Post: ninjaisfast
  Checking for equality PythonGainz 8 3,242 Apr-10-2020, 03:00 PM
Last Post: PythonGainz
  Equality in Python el_bueno 8 3,433 Feb-08-2020, 03:33 PM
Last Post: el_bueno
  Divisors shared the second numbers mircea_dragu 1 2,043 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  Dictionnaries - Equality SupaFlamme 3 2,728 Jan-13-2019, 04:50 PM
Last Post: perfringo
  Problems with Equality of Numbers SheeppOSU 3 2,384 Jan-04-2019, 07:34 AM
Last Post: Gribouillis
  running just one process shared among uses Skaperen 3 2,988 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Shared queues l00p1n6 3 2,990 May-15-2018, 01:38 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020