Custom exceptions with kwargs (Python) -


is safe?

class specialalert(exception):      def __init__(self, message, **kwargs):         exception.__init__(self, message)         kw in kwargs:             if kw == 'message':                 continue             setattr(self, kw, kwargs[kw]) 

related to: proper way declare custom exceptions in modern python?

butt.. safe? mean i'm overriding attributes of self?

in specialalert want pass kinds of things (this exception done doing long series of different checks, "try..except specialalert error" in loop , details out of error in handlers)

i have basic check if i'm not overriding "message", should skip setting other attrs on self too? they?

this work fine; it's cromulent put additional attributes on object. however, rather checking standard attributes don't want clobber, call exception.__init__ last, , let clobber yours if accidentally set them. __init__() can be:

def __init__(self, message, **kwargs):     self.__dict__.update(kwargs)     exception.__init__(self, message) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -