[Tutor] Guidance on using custom exceptions please
Danny Yoo
dyoo at hashcollision.org
Tue Oct 13 15:27:11 EDT 2015
> Then it seems that an importer of that module must include the module name when referencing the exceptions:
>
> import TxControl
>
> try:
> send(msg)
> except (TxControl.MessageTimeoutError, TxControl.UndefinedMessageTypeError) as err:
> # Exception processing
>
> Including 'TxControl' seems a bit tedious, and is even worse if TxControl imports exceptions from yet another module and allows them to pass up the stack.
>
> How would you handle this situation stylistically?
Stylistically, I'd keep it. This doesn't look bad to me. Certain
style guides encourage this, because it becomes easier to see where
values are coming from. One definite perk is that it avoids
conflicting names from different packages.
For example, here's one recommendation:
https://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Packages#Packages
(Commentary: I've been in a world where imports were unqualified by
default, and it was a mess. My programs grew large enough that it
made name conflicts almost inevitable. Qualified names are lengthy,
but they make that problem a non-issue.)
More information about the Tutor
mailing list