Method returns self?
Roy Smith
roy at panix.com
Thu Oct 18 22:49:51 EDT 2001
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.
Now, I realize, I could just have it return None instead of self, and then
use it like:
parser = myParser()
data = parser.parse(filename)
try:
data.verify()
except data.VerifyError:
print "you bozo"
This would avoid the problem entirely, but it's not as interesting to think
about :-)
More information about the Python-list
mailing list