Author robcliffe
Recipients CuriousLearner, Rodolpho.Eckhardt, belopolsky, brian.curtin, eric.araujo, georg.brandl, henriquebastos, ncoghlan, robcliffe, ron_adam, terry.reedy
Date 2018-07-15.03:16:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <aacafde0-464d-88bb-6cf0-d4c922a85561@btinternet.com>
In-reply-to <1531572258.04.0.56676864532.issue8525@psf.upfronthosting.co.za>
Content
On 14/07/2018 13:44, Nick Coghlan wrote:
> Nick Coghlan <ncoghlan@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
History
Date User Action Args
2018-07-15 03:16:28robcliffesetrecipients: + robcliffe, georg.brandl, terry.reedy, ncoghlan, belopolsky, ron_adam, eric.araujo, brian.curtin, henriquebastos, Rodolpho.Eckhardt, CuriousLearner
2018-07-15 03:16:28robcliffelinkissue8525 messages
2018-07-15 03:16:27robcliffecreate