style query: function attributes for return codes?

holger krekel hpk at trillke.net
Fri Dec 10 14:57:00 EST 2004


[Reinhold Birkenfeld Fri, Dec 10, 2004 at 08:42:10PM +0100]
> holger krekel wrote:
> >     class Connection(object): 
> >         def __init__(self, **kw): 
> >             for name in kw: 
> >                 assert name in ('good', 'badauth', 'noserver'), name 
> >                 setattr(self, name, kw[name]) 
> > 
> >     def get_connection():
> >         if tcp_conn():
> >             if server_allows_conn():
> >                 return Connection(good=True) 
> >             else:
> >                 return Connection(badauth=True) 
> >         else:
> >             return Connection(noserver=True) 
> 
> That's evil, because "if conn.good" raises an AttributeError instead of
> evaluating to False if the connection is not good. You would have to
> inizialize all three attributes in every construction of a Connection
> object.

Ups, you are right of course.  I somehow managed to delete the line ... 

    class Connection(object): 
        good = badauth = noserver = False   

thanks for pointing it out. 

    holger



More information about the Python-list mailing list