Hello all - I come here seek assistance due to a problem . By dint of searching, I managed to bring the problem to a simple example which will serve as a demo easily understandable
I manage a text object. For this purpose there are various methods call me to get info on it as nb lines, words, etc..
At one point, I must submit a paragraph of text. I say to myself "a paragraph is a mini-text, if I return a new instance of my object does not contain that paragraph, this body will have all useful methods (number of lines, words, etc.).
Here is a small simple example showing the problem. For simplicity, I just created an object that stores a table of numbers and created a "spell" that returns an object containing the table sorted Code:
#! / usr / bin / env python
# Coding: Latin-1 -*-
cGestion class:
Def __init__ (self):
Print "init:% s"% repr (self)
Self.tab = []
Def __del__ (self):
Print "del% s"% repr (self)
Del self.tab [:]
Del self.tab
Def __str__ (self):
return "[% s]:% s"% (repr (self), self.tab)
Def append (self, k):
Self.tab.append (k)
Def sort (self):
N = cGestion ()
For k in self.tab:
N.append (k)
N.tab.sort ()
Return n
# Create basic element
cGestion base = ()
base.append (2)
base.append (1)
base.append (0)
print "\ nbase:", base
Test # 1: sorted copy of the basic object - a copy is stored
cp = base.sort ()
print "\ ntest 1:", cp, cp.tab
del cp
Test # 2: just display a sorted
print "\ ntest 2:", base.sort ()
Test # 3: display of an element of the body sorted
print "\ ntest 3:", base.sort (). tab
At the base, I create my object. The displays are ok
In test 1, I get a copy of my item sorted. No worries
In test 2, I am just copying sorted my object but have not saved. But no worries
In test 3, where I do a table stored in the copy when the copy and I am aware that the destroyer has made his office and the table is empty.
What is interesting is that if in the destructor delete the cleaning of the table (del self.tab [:]), is all going well. I know that Python has a memory management and that I should not worry about me release my internal table but I'm from the C and it's stronger than me. If I create a constructor, destructor deletes my element.
Someone would give me advice?
Thank you
Bookmarks