1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
from enthought.traits.api import Delegate, HasTraits, Instance, Int, Str
class Parent(HasTraits):
# INITIALIZATION: last_name' is initialized to '':
last_name = Str('')
class Child(HasTraits):
age = Int
# VALIDATION: 'father' must be a Parent instance:
father = Instance(Parent)
# DELEGATION: 'last_name' is delegated to father's 'last_name':
last_name = Delegate('father')
# NOTIFICATION: This method is called when 'age' changes:
def _age_changed(self, old, new):
print 'Age changed from %s to %s ' % (old, new)
# Set up the example:
joe = Parent()
moe = Child()
moe.father = joe
print "-------------"
import sys
import weakref
# create reference faible
joeRef = weakref.ref(joe)
moeRef = weakref.ref(moe)
# nbre reference
print "joe refcount", sys.getrefcount(joe)
print "moe refcount", sys.getrefcount(moe)
#
print "reset_traits"
joe.reset_traits()
moe.reset_traits()
# nbre reference
print "joe refcount", sys.getrefcount(joe)
print "moe refcount", sys.getrefcount(moe)
joe = None
moe = None
print "-------------"
# récupére l'objet à partir de la référence
joe = joeRef()
if joe:
print joe, sys.getrefcount(joe)
else:
print "joe is None"
# récupére l'objet à partir de la référence
moe = moeRef()
if moe:
print moe, sys.getrefcount(moe)
else:
print "moe is None" |
Partager