[Python-Dev] When to signal an error
Paul Prescod
paul@prescod.net
Sun, 20 Jan 2002 18:27:59 -0800
"Martin v. Loewis" wrote:
>
>...
>
> > 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.
Just for the record, I think that Jason's rewrites were clearer in every
case because they said exactly what he was trying to do.
"If the sys module has the attribute ps1 then ..."
This is much clearer than "Get the ps1 attribute from the sys module and
throw it away.".
Python has a functions specifically for checking for the existance of
attributes and keys. Why not use them?
Plus, I think that exceptions should be (as far as possible) reserved
for exceptional situations. Using them to as tests is not as compact,
not as readable and not as runtime efficient.
But more to the point, any of these could have been rewritten as:
_junk = sys.ps1
That would shut up compiler messages without forcing you to use the
haskey/hasattr style.
Paul Prescod