Please keep discussion on the list......

> > I'm not sure if I understand your question correctly but maybe this will
> > help:
> >
> > If you want code to be run upon creating an instance of your class you
> > would use __init__. Most common examples include setting attributes on
> > the instance and doing some checks, e.g.
> >
> > class Person:
> >    def __init__( self, first, last ):
> >        if len( first ) > 50 or len( last ) > 50:
> >            raise Exception( 'The names are too long.' )
> >        self.first = first
> >        self.last = last
> >
> > And you would use your class like so,
> >
> > p1 = Person( 'John', 'Smith' )
> > p2 = Person( "Some long fake name that you don't really want to
> > except, I don't know if it's really longer than 50 but let's assume it
> > is", "Smith" )
> > # This last one would raise an exception so you know that something is not
> > okay
> >
> > HTH,
> > Daniel
>
> Is not the code run when I create an instance by assignement somewhere else?
>
> I take the point that one might want to check for potential exceptions
> immediately, but most examples in the literature aren't doing that and don't
> seem to be doing anything that would not be done when creating an instance
> by assignment later somewhere. I'm missing something basic here.

What do you mean by "create an instance by asignment somewhere else"?
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to