News for hackers. Stuff that matters.
Re:The Descriptor Protocol and Python Black Magic by avyfain on Tuesday May 10, 2016 @05:49PM (#11670950)
Author here. Questions/comments/feedback are very much appreciated! AMA
Re:The Descriptor Protocol and Python Black Magic by re on Tuesday May 10, 2016 @06:17PM (#11671136)
Very minor point: be careful comparing IDs the way you do in the REPL (i.e., without keeping a reference to the previous object)--since the old one is released, it's possible that the memory address is reused and the ID of the new object ends up being the same, even though the object is different, which can have misleading results:

    Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class A:
    ...   def b(self): pass
    ... 
    >>> A.b
    <unbound method A.b>
    >>> hex(id(A.b))
    '0x104e2c0a0'
    >>> hex(id(A.b))
    '0x104daeb90'
    >>> hex(id(A.b))
    '0x104daeb90'
So the `x is y` approach is "safer." :)
Re:The Descriptor Protocol and Python Black Magic by avyfain on Tuesday May 10, 2016 @06:49PM (#11671313)
Interesting, had not considered that at all. So basically, to ensure the old reference is not released, just naming it would do the trick? AKA:

    >>> some_unbound_method = A.b
    >>> hex(id(some_unbound_method))
    '0x104e2c0a0'
Re:The Descriptor Protocol and Python Black Magic by phasmantistes on Tuesday May 10, 2016 @07:34PM (#11671530)
Yep, that's right. To lay out the example the way you did in the article, it would look like:

    >>> foo = A.b
    >>> bar = A.b
    >>> hex(id(foo))
    '0xdeadbeef'
    >>> hex(id(bar))
    '0xdecafcab'
    >>> foo is bar
    False