[Python-Dev] When to signal an error

Martin v. Loewis martin@v.loewis.de
Sun, 20 Jan 2002 01:02:10 +0100


> Each one could be rewritten (I'm curious as to the prevailing
> stylistic opinions on this):

I think those rewrites do not improve the code, see detailed comments
below.

> Could be rewritten:
>         if not hasattr(sys, 'ps1'):
>             sys.ps1 = ">>> "
>         if not hasattr(sys, 'ps2'):
>             sys.ps2 = "... "

Using string literals when you mean attribute names is bad style. It
just helps to trick the checker. Sometimes, you cannot avoid this
style, but if you can, you should.

> if globals().has_key("LC_MESSAGES"):
>     __all__.append("LC_MESSAGES")

This combines the previous issue with the usage of globals(). I find
it confusing to perform function calls to check for the presence of
names.

> try:
>     UnicodeType
> except NameError:
>     UnicodeType = None
> 
> Could be rewritten:
> globals().setdefault('UnicodeType', None)

Same issue here. If this needs to be rewritten, I'd prefer

try:
    from types import UnicodeType
except ImportError:
    UnicodeType = None

Somebody might also change the "from types import *" to explicitly
list the set of names that are requested, when changing this fragment.

Regards,
Martin