Garbage collection and Method redefinition

Jesus Cea Avion jcea at argo.es
Tue Sep 19 18:37:07 EDT 2000


Python 1.5.2 here.

I have the following class:

class a:
  def __init__(self):
    self.func=self.func2
  def func(self):
    print "func"
  def func2(self):
    print "func2"
  def __del__(self):
    print "destroyed"

Doing "b=a(); b.func()" prints "func2", correctly.

My problem, nevertheless, is garbage collection: when you override a
method in this way (or, for example, b.new=b.old), the refcounter is
incremented and the instante won´t be garbage collected:

>>> b=a()
>>> del b
>>> b=a()
>>> b.__del__()
destroyed
>>> print b
<__main__.a instance at 12bf20>

Is this a bug?

I need dynamic method overriding in order to improve the performance of
some of my classes. For example "a.func()" has an initial
implementation; when some events occurs, "a.func()" is redefined in
order to skip some tests and internal method calls.

What am I doing wrong?.

-- 
Jesus Cea Avion                         _/_/      _/_/_/        _/_/_/
jcea at argo.es http://www.argo.es/~jcea/ _/_/    _/_/  _/_/    _/_/  _/_/
                                      _/_/    _/_/          _/_/_/_/_/
PGP Key Available at KeyServ   _/_/  _/_/    _/_/          _/_/  _/_/
"Things are not so easy"      _/_/  _/_/    _/_/  _/_/    _/_/  _/_/
"My name is Dump, Core Dump"   _/_/_/        _/_/_/      _/_/  _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz



More information about the Python-list mailing list