Sure you can (although it forces the user to enclose any instantiation on a
try except, but it works). I guess my point is that validation is usually
done in the constructor and not initialisation, creating an instance just
to destroy it later on without doing any work (contributing to solve the
problem the programmer is trying to solve) is a bit wasteful and I would
not recommend it as good practice to someone trying to learn.

On 29 Oct 2016 1:21 PM, "Alok Gandhi" <alok.gandhi2...@gmail.com> wrote:

> 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 python_inside_maya+unsubscr...@googlegroups.com.
> 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
> <https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMS434aqjf8AMrLVZTvE%3DCJxXXN7-ChHN%3DMd%2B8wG-6v1%2Bg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-KnYYb3f1OOWEcMJMmjHeKmy1jzUSvp0hHmZuc%3DsmT9g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to