Method returns self?
Gerhard Häring
gh_pythonlist at gmx.de
Fri Oct 19 00:41:23 EDT 2001
On Thu, Oct 18, 2001 at 11:36:46PM -0400, Ignacio Vazquez-Abrams wrote:
> On Thu, 18 Oct 2001, Roy Smith wrote:
>
> > I've got a data file parser that returns a data object. You call it
> > something like this:
> >
> > parser = myParser()
> > data = parser.parse(filename)
> >
> > Now, I want to give the data class a verify() method. The parser only
> > ensures that the data file has a parsable syntax, but there are also a
> > number of higher-level semantic checks that we might want to do. I have in
> > mind something that you would call like this:
> >
> > parser = myParser()
> > try:
> > data = parser.parse(filename).verify()
> > except data.VerifyError:
> > print "you bozo"
> >
> > The idea is that verify() is a method of the data class which either
> > returns self, or raises an exception. The code would look something like:
> >
> > class data:
> > def verify (self):
> > if everthing is cool:
> > return self
> > else:
> > raise VerifyError
> >
> > My question is, will I run into garbage collection or reference count
> > problems if a method returns self? I can't quite put my finger on it, but
> > I have this vague feeling I might end up with a self-referential object.
You should only be getting refcount problems when you have circular
references, like that:
def foo():
x = {}; y = {}; x[1] = y; y[1] = x
x and y are referencing each other and normally won't be collected when
foo() returns. But they will be if you build your Python with
"--with-cycle-gc". I don't know if this option is already activated by
default.
As Ignacio already said, in your case there are no circular references.
Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://www.cs.fhm.edu/~ifw00065/ OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20 A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))
More information about the Python-list
mailing list