[issue8525] Display exceptions' subclasses in help()
Rob Cliffe
report at bugs.python.org
Sat Jul 14 23:16:28 EDT 2018
Rob Cliffe <rob.cliffe at btinternet.com> added the comment:
On 14/07/2018 13:44, Nick Coghlan wrote:
> Nick Coghlan <ncoghlan at gmail.com> added the comment:
>
> Reviewing the builtins in 3.7, I get the following results for builtin objects that have defined subclasses immediately after interpreter startup:
>
> =============
>>>> for name, obj in vars(builtins).items():
> .. if isinstance(obj, type) and name in str(obj):
> .. subclasses = type(obj).__subclasses__(obj)
> .. if subclasses:
> .. print(f"{obj}: {len(subclasses)}")
> ..
> <class 'classmethod'>: 1
> <class 'dict'>: 1
> <class 'property'>: 1
> <class 'int'>: 1
> <class 'object'>: 132
> <class 'staticmethod'>: 1
> <class 'tuple'>: 16
> <class 'type'>: 1
> <class 'BaseException'>: 4
> <class 'Exception'>: 19
> <class 'ImportError'>: 2
> <class 'OSError'>: 13
> <class 'RuntimeError'>: 3
> <class 'NameError'>: 1
> <class 'SyntaxError'>: 1
> <class 'IndentationError'>: 1
> <class 'LookupError'>: 3
> <class 'ValueError'>: 2
> <class 'UnicodeError'>: 3
> <class 'ArithmeticError'>: 3
> <class 'SystemError'>: 1
> <class 'Warning'>: 10
> <class 'ConnectionError'>: 4
> =============
>
> So rather than special-casing exceptions or builtins in general, my inclination would be to include a section that lists up to 4 subclasses inline, and then adds a "... and NNN additional subclasses" trailing when there are more than 4 (or when there are less than 4 subclasses with public names, but additional private subclasses).
>
>
My 2 cents:
To use Exceptions optimally (e.g. to make the errors you trap
neither too specific nor too general), you often need to know (and
understand) the relevant part of the Exception hierarchy. In particular
you may know the name of an Exception that covers a particular use case,
but not the names of its subclasses, one of which might be more
appropriate. Exceptions are exceptional* in that the "issubclass"
relationship is vital to the way that they work. So it is USEFUL to
know ALL subclasses of a given Exception class (not just 4; in practice
there won't be more than a dozen or two). I have found myself in just
this position; in fact it impelled me to adding a "show subclasses"
feature to help(<anyExceptionClass>) in my then current version of
Python 2. (An alternative might be a handy way of showing the complete
built-in Exception hierarchy.)
I question whether it is really useful to know all subclasses of ANY
class, or whether YAGNI. I think that, for non-Exception classes,
typically when you look at a class you may want to know its inheritance
(to understand its functionality better), but it is rare that you will
want to know what subclasses it has, if any. No doubt there are
exceptions* (perhaps you monkey-patch a class and want to know what
subclasses might be affected).
Regards
Rob Cliffe
* Pun not intended
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue8525>
_______________________________________
More information about the Python-bugs-list
mailing list