Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Below is a summary of equality by reference and value in different programming languages.
import pandas as pd
s = """
Reference Value
Python is ==
R [1] ==
Java == equals
Scala eq [2] == or equals [3]
"""
data = [line.split("\t") for line in s.split("\n") if line != ""]
pd.DataFrame(data[1:], columns=data[0])Loading...
Tips and Tricks¶
[] and [] are 2 indepedent (empty) list objects with different address.
However,
() and () are the same (empty) tuple singleton.
The reason is that list is mutable while tuple is immutable.
[] is []False[] == []True() is ()True() == ()TrueBelow are more examples.
a = [1, 2, 3]
b = a[:]
a is bFalsefrom copy import copy
a = [1, 2, 3]
copy(a) is aFalsec = (1, 2, 3)
d = c[:]
c is dTruec = (1, 2, 3)
copy(c) is cTruec = (1, 2, 3)
e = (1, 2, 3)
c is eFalseNotice that since str is immutable (similar to tuple) in Python,
it has similar behavior on equaility to tuple.
f = "ABC"
g = "ABC"
f is gTruef = "ABC"
copy(f) is fTruef = "ABC"
h = f[:]
f is hTrue