Exception problem with module

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue May 13 20:02:54 EDT 2014


On Tue, 13 May 2014 16:59:46 +0000, Joseph L. Casale wrote:

> I am working with a module that I am seeing some odd behavior.
> 
> A module.foo builds a custom exception, module.foo.MyError, its done
> right afaict.
> 
> Another module, module.bar imports this and calls
> bar.__setattr__('a_new_name', MyError).

I see that you've solved your immediate problem, but you shouldn't call 
__setattr__ directly. That should actually be written

    setattr(bar, 'a_new_name', MyError)

But really, since bar is (apparently) a module, and it is *bar itself* 
setting the attribute, the better way is

a_new_name = MyError

or even 

from module.foo import MyError as a_new_name



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list