This code produces a different output in Python 2and Python 3.
Python 2
Python 3
class Descriptor(object): def __get__(self, instance, owner): print('read') return 1 def __set__(self, instance, value): print('write') def __delete__(self, instance): print('del') class C(): a = Descriptor() c = C() c.a c.a = 3 del c.a c.a print('finished')
Output for Python 2:
read read finished
For Python 3, this is:
read write del read finished
Why does this work? How do descriptors Python 2differ from descriptors Python 3?
This doesn't make sense either, because http://docs.python.org/release/3.0.1/reference/datamodel.html#invoking-descriptors clearly describes the same thing as http://docs.python.org /reference/datamodel.html#invoking-descriptors
(This is the documentation for Python 2.7and Python 3.0.)
Python 2.7
Python 3.0
: Ned Deily , C - Python 2, object .
C
object
Python 2 c.a, c.a = 3, , c.a.
c.a
c.a = 3
c = C() c.a c.a = 3 print c.__dict__['a'] print C.__dict__['a'] del c.a c.a
:
read 3 <__main__.Descriptor object at 0x04625570> read finished