>
> There's a fundamental problem with this in my view, the dounder init
> method of a class gets executed _after_ the instance (aka self) get
> created, doing checks there means you basically create an object that can
> potentially be invalid by passing the wrong thing and that's a problem.

I don't see this as a problem. The __init__() method is for initialization
of the instance. If something goes wrong during the initialization, you can
always raise from within __init__(). I mean, whatever sanity checks you
want to put in for 'filling in' the variables should happen in __init__()
because that is the primary role of this method. If anything bad happens
and the object becomes 'invalid' python will call __del__() to clean it
after itself. So it is perfectly safe to raise from __init__().

Here's a snippet exhibiting the call to __del__() by python's memory
management when __init__() fail:

class A(object):
    def __init__(self):
        raise ValueError('Something bad happened!')

    def __del__(self):
        print "Delete Called"

def main():
    try:
        a = A()
    except ValueError, e:
        print e
if __name__ == '__main__':
    main()


Would result in:
>>>Something bad happened!
>>>Delete Called

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMS434aqjf8AMrLVZTvE%3DCJxXXN7-ChHN%3DMd%2B8wG-6v1%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to