I'm going to assume you are intentionally comparing the method objects themselves—and not that you really wanted to compare the output strings and just forgot to put ()
after sayHello
.
Try this experiment:
a = Person()
b = Person()
a.sayHello
b.sayHello
You'll see that a.sayHello
displays as something like
>
...whereas b.sayHello
displays similarly but with a different parent instance pointer:
>
The bound method of one instance of a Person
is itself a different instance (of a method) from the bound method of the same name from a different Person
instance. You can confirm this with id(a.sayHello)
and id(b.sayHello)
which return the identity hashes of the two respective bound methods—they'll be different. Since your code Person().sayHello is Person().sayHello
creates two different Person
instances on the fly, the situation is the same as with my named instance examples a
and b
.