LBYL vs EAFP
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Feb 5 01:31:22 EST 2013
On Tue, 05 Feb 2013 16:20:19 +1100, Chris Angelico wrote:
> On Tue, Feb 5, 2013 at 3:52 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> There's also the principle that it is best to raise an exception as
>> early as possible. It's easier to track down errors at the point they
>> are introduced than long afterwards.
>
> Yes, definitely, especially (as was mentioned) if you're working with
> callbacks. But I'd use isinstance then.
I'm leaning towards an isinstance check
I've been using Python since Python 1.5. Even though 1.5 had an
"isinstance" function, I learned Python from books and code written for
Python 1.4 which did not have that function, so the usual way to do type-
checking was:
if type(obj) is type(1):
# it's an int
and strongly discouraged. So my instincts are still very strongly primed
to *not* do type-checking if I can avoid it. But in this case, I think I
agree with those suggesting the isinstance check is the right approach.
The main downside to this is that objects which delegate to a number will
not work unless they are explicitly registered with the Number ABC.
Thanks to everyone who responded.
--
Steven
More information about the Python-list
mailing list