цифра, которую я опубликую, чем закончил:
import weakref
class S(object):
def __init__(self):
self._r = -1
@property
def r(self):
try:
return self._c.r
except (AttributeError, ReferenceError):
return self._r
@r.setter
def r(self, value):
try:
self._c.r = value
except (AttributeError, ReferenceError):
self._r = value
class C(object):
def __init__(self, s):
self._r = -2
s._c = weakref.proxy(self)
self.s = s
@property
def r(self):
return self._r
@r.setter
def r(self, value):
self._r = value
def printRs(s, c):
print "s.r", s.r
if c:
print "c.r", c.r
print "c.s.r", c.s.r
s = S()
c = C(s)
printRs(s, c) # => -2 on all
c.r = 0
printRs(s, c) # => 0 on all
s.r = 1
printRs(s, c) # => 1 on all
c.s.r = 2
printRs(s, c) # => 2 on all
c = None
printRs(s, c) # => -1 on s.r