getattr and method name
Roy Smith
roy at panix.com
Sun Oct 2 22:12:40 EDT 2011
In article <mailman.1677.1317604930.27778.python-list at python.org>,
Gary Herron <gherron at islandtraining.com> wrote:
> On 10/02/2011 05:24 PM, Kevin Walzer wrote:
> > Turns out the error was a typo in the actual method being
> > called...*faceinhands*
> >
> > Sorry for the noise.
> >
>
> But this is a great example of why you should not use a naked except
> clause. As stated, your code will execute the except clause for *any*
> kind of an error, not just the exception you envisioned when you wrote
> it. If you had written the except clause to catch just the exceptions
> you were interested in, then the exception int a called function would
> have come through that code as in un-handled exception, instead of being
> caught and essentially ignored.
And, along those same lines, a couple of bits of generic exception
advice...
1) Create specific exceptions to describe specific problems.
FooPackageSocketBindError sure beats IOError when it comes to trying to
figure out what went wrong.
2) As much as possible, keep your try blocks short. In the original
example, we've got:
try:
functionstring = args[2]
callfunction = getattr(self, functionstring.split('.')[1])
self.passtext = self.tk.call('authorize::getAuthPassword')
callfunction()
If we caught an IndexError, we would not know if it came from the
args[2], or the getattr(...)[1], or possibly even from something deep
down within callfunction().
More information about the Python-list
mailing list