Steven W. Orr wrote: > When I go to create an object I want to be able to decide whether the > object is valid or not in __init__, and if not, I want the constructor to > return something other than an object, (like maybe None). I seem to be > having problems. At the end of __init__ I say (something like) > > if self.something < minvalue: > del self > return None > > and it doesn't work. I first tried just the return None, then I got crafty > and tried the del self. Is what I'm trying to do possible in the > constructor or do I have to check after I return? Or would raising an > exception in the constructor be appropriate? > > Am I even being clear? > The trouble you have is that it's too late by the time you get to __init__. The object has been created. The reason that "del self" doesn't work is that all it does is remove the local name "self" from the namespace of the method - you will find that if __init__ returns anything *except* None you get an exception.
Don't think of __init__ as a constructor - that's __new__, which *is* expected to return a newly-created instance. Raising an exception in __init__ is perfectly permissible, but adopting the new-style classes (inheriting from object) might give you a more efficient solution to your problem. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden Recent Ramblings http://holdenweb.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list