In article <[EMAIL PROTECTED]>,
Grant Edwards <[EMAIL PROTECTED]> wrote:
>On 2006-05-19, bruno at modulix <[EMAIL PROTECTED]> wrote:
>
>>> Either deal with the resulting NameError exception (EAFP[0])
>>
>> try:
>> getattr(commands, VARIABLE)()
>> except NameError:
>> print >> sys.stderr, "Unknown command", VARIABLE
>>
>>> or test
>>> first whether the attribute exists (LBYL[1]).
>>
>> command = getattr(commands, VARIABLE, None)
>> if command is None:
>> print >> sys.stderr, "Unknown command", VARIABLE
>> else:
>> command()
>>
>> I'd go for the first solution.
>
>Me too. Assuming the user isn't clueless, the normal case is
>where the command exists. Write code for the normal case and
>use the exception that occurs for exceptional cases.
.
.
.
Guys, I try--I try *hard*--to accept the BetterToAskForgiveness
gospel, but this situation illustrates the discomfort I consistently
feel: how do I know that the NameError means VARIABLE didn't resolve,
rather than that it did, but that evaluation of commands.VARIABLE()
itself didn't throw a NameError? My usual answer: umm, unless I go
to efforts to prevent it, I *don't* know that didn't happen.
--
http://mail.python.org/mailman/listinfo/python-list