6

Code:

class Foo(object):
    pass

foo = Foo()
foo.__init__ == foo.__init__ #return true
foo.__init__ is foo.__init__ #return false

I can understand foo.__init__ == foo.__init__ returns True. Why foo.__init__ is foo.__init__ return False?

Grijesh Chauhan
  • 52,958
  • 19
  • 127
  • 190
shoujs
  • 1,053
  • 1
  • 11
  • 22
  • 2
    This is interesting. I've seen a lot of `is vs ==` questions but this one is a bit different. +1. – aIKid Jan 09 '14 at 06:23
  • 2
    @delnan Do you know why `id(f.__init__) == id(f.__init__)` evaluates to `True` in this case? – aIKid Jan 09 '14 at 06:25
  • 3
    @aIKid It's not necessarily true, and when it's true it's true for every other method too. Try it. The reason is probably explained in some other question on the subject. In short: Both `id` calls are evaluated separately, both construct a new bound method object and get its `id` then immediately drop it. Due to refcounting the objects are immediately freed and due to the inner workings of the memory allocators the same space (which in CPython means the same ID) is used for the other. –  Jan 09 '14 at 06:29
  • @delnan Can you post an answer here. or give us a link. – Grijesh Chauhan Jan 09 '14 at 06:30
  • 1
    @GrijeshChauhan Answer for what? The original question or alKid's follow up? –  Jan 09 '14 at 06:30
  • @delnan @ alKid's follow up – Grijesh Chauhan Jan 09 '14 at 06:31
  • I feel like the bigger question is, "Why would you do this?" It seems like a huge code smell. – jpmc26 Jan 09 '14 at 06:31
  • @jpmc26 For fun and for an extra knowledge, of course! :D – aIKid Jan 09 '14 at 06:32
  • 1
    @GrijeshChauhan http://stackoverflow.com/q/13348031/395760 –  Jan 09 '14 at 06:34

0 Answers0