[Python-3000] PEP 3100 Comments
Steven Bethard
steven.bethard at gmail.com
Tue May 9 04:51:35 CEST 2006
On 5/8/06, Guido van Rossum <guido at python.org> wrote:
> On 5/8/06, Steven Bethard <steven.bethard at gmail.com> wrote:
> > It'd certainly be nice to be able to tell the difference between
> > the following two TypeErrors:
> >
> > >>> def s():
> > ... raise TypeError()
> > ...
> > >>> 's'()
> > Traceback (most recent call last):
> > File "<interactive input>", line 1, in ?
> > TypeError: 'str' object is not callable
> > >>> s()
> > Traceback (most recent call last):
> > File "<interactive input>", line 1, in ?
> > File "<interactive input>", line 2, in s
> > TypeError
>
> You're kidding yourself. Consider these two:
>
> def s():
> raise NotCallable("ha ha, fooled you!")
This one does exactly what I would hope it to. The writer of the
callable has indicated that this function is not to be called by
raising NotCallableError. This would be especially appropriate in a
subclass that wants to break substitutability and remove the __call__
method. Sure, people could abuse it, but we're all supposed to be
adults here, right?
> or more likely any variant of this:
>
> def s():
> 's'()
Sorry if I came across as suggesting that adding NotCallableError
would solve all the world's problems ;-) If you caught a
NotCallableError, you would know that either the object you called was
not callable, or it called some other object that was not callable and
didn't trap the exception. That's a fair bit more informative than
the current situation with TypeError, since a lot more things raise
TypeErrors:
>>> iter(1)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence
>>> 'abc'['1']
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: string indices must be integers
>>> str(1, 16)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
>>> 42 + '56'
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'
So if I catch a TypeError, it could be because the object I called was
not callable, or it could be because when calling that object, any one
of the above TypeErrors was raised.
Still, like you say, NotCallableError's not a silver bullet.
STeVe
--
Grammar am for people who can't think for myself.
--- Bucky Katt, Get Fuzzy
More information about the Python-3000
mailing list