Ten kod wytwarza inne wyjście w postaci Python 2
i Python 3
.Deskryptory w języku Python nie działają w języku Python 2.7
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')
Wyjście dla Pythona 2 jest:
read
read
finished
dla Pythona 3 to:
read
write
del
read
finished
Dlaczego to działa w ten sposób? Czym różnią się deskryptory Python 2
od deskryptorów ?
To również sprawia, że nie ma sensu, ponieważ http://docs.python.org/release/3.0.1/reference/datamodel.html#invoking-descriptors jasno opisuje dokładnie takie same jak http://docs.python.org/reference/datamodel.html#invoking-descriptors
(Są dokumentacje dla Python 2.7
i Python 3.0
.)
(BTW, Python 3.0 i jego dokumentacja są przestarzałe i na emeryturze, nie używać Pythona 3.0 lub 3.0.1 Obecne docs są na stronie http:. // docs. python.org/py3k/ i obecna wersja to 3.2.3.) –