2

I'm practicing for a midterm, and I came across this:

the_cake = [1,2,[3],4,5]
a_lie = the_cake[1:4]
the_cake = the_cake[1:4]
great = a_lie
delicious = the_cake
moist = great[:-1]

After running this code in the Python interpreter, why is:

the_cake.append == a_lie.append
False

My thought is that they are equal methods, and though not "IS", should fulfill the equality.

Maybe this evaluates to False because of instantiation? If this is true, then do class attributes evaluate to True when compared? Is this a special case with list objects?


Follow-up: According to this: Is there a difference between `==` and `is` in Python?

"IS will return True if two variables point to the same object, == if the objects referred to by the variables are equal."

Then do methods of the List class point to separate instances of the "append" method?

So if I define a function x(parameter), every time I call it, it'll be the same because it's the same object assigned to different variables, right?

Then for some equivalent variable "parameter":

x(parameter) == x(parameter)
True

Thanks!

Community
  • 1
  • 1
Dave Liu
  • 906
  • 1
  • 11
  • 31
  • I would wager this is because of the different objects (and their different locations) of which they are attached – kylieCatt Oct 27 '14 at 00:07
  • Python compares function memory addresses. [How does python compare functions?](http://stackoverflow.com/questions/7942346/how-does-python-compare-functions) – Mr. Polywhirl Oct 27 '14 at 00:09
  • @DaveL I am very glad I could be of any help. Could you also upvote my answer since you accepted it? thank you. – kolonel Oct 27 '14 at 00:46

4 Answers4

2

The methods are at different locations along with their respective object instances. For instance we have:

a = []
b = []

So we have:

>>> a.append == b.append
False

and their respective locations is given in:

>>> a.append
<built-in method append of list object at 0x7f7c7c97d560>
>>> b.append
<built-in method append of list object at 0x7f7c7c97d908>

Notice the different addresses.

kolonel
  • 1,412
  • 2
  • 16
  • 33
1

Python 2.x: Functions of type objects implement rich comparisons based on the object's address in memory.

Python 3.x: be careful that functions are not longer orderable. So, e.g., the_cake.append > a_lie.append will throw an error message.

gmas80
  • 1,218
  • 1
  • 14
  • 44
1

Both answers are valid, but check this out too:

>>> a = []
>>> b = a
>>> a.append == b.append
True
nathancahill
  • 10,452
  • 9
  • 51
  • 91
1

Slicing of list in python always returns a new list. the_cake[1:4] returns a new list as well. So if you call the same slice each time that does not mean that it will return the same list. Irrespective of whether you do the same slice again and again, it will return a new list each time it is being called.

Even though you are assigning the same slice the_cake[1:4] to a_lie as well as to itself(i.e. the_cake), both are referring to a new list which is different from the other one. So both the list have a different memory location assigned during creation. If you check id(the_cake) == id(a_lie), it will return False.

So now when you refer to append for both of the instances, they also differ as well. Even though same method is being referred, it is referred from two different instances. So it will create different instances as well for the method being invoked. Therefore the instances referred when called the_cake.append differs from that of a_lie.append.

Pabitra Pati
  • 457
  • 4
  • 12