This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tuple arg for issubclass
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: doerwalter Nosy List: doerwalter, loewis
Priority: normal Keywords: patch

Created on 2002-12-06 16:32 by doerwalter, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
diff.txt doerwalter, 2002-12-06 16:32
Messages (6)
msg41887 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-12-06 16:32
The following patch enhances issubclass(), so that it's
possible to pass a tuple as the second argument. This
has the same meaning as for isinstance(): the first
argument will be checked against each entry in the tuple.

The patch works somewhat differently than isinstance,
because for isinstance, a tuple argument is unpacked
recursively, this patch doesn't do this, instead any
entry in the tuple that isn't a class raises a TypeError.
msg41888 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-07 08:42
Logged In: YES 
user_id=21627

-1. I find this confusing: one may expect that issubclass(a,
(b,c)) is true iff a is a subclass of both b and c. In the
face of ambiguity, refuse the temptation to guess.

What is the rationale for this feature, apart from symmetry
with isinstance?
msg41889 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-12-07 11:57
Logged In: YES 
user_id=89016

I don't see why this should be more confusing than a tuple 
argument for isinstance.

Guido himself was wondering whether he should implement 
this feature (see http://mail.python.org/pipermail/python-
dev/2001-October/017813.html), but it was dropped because 
of to much work and to few benefits. The work is done and 
there there same benefits as with isinstance. In my own code 
I often have to do the following check:

if isinstance(x, type) and (issubclass(x, Element) or 
issubclass(x, ProcInst) or issubclass(x, Entity) or issubclass
(x, CharRef) or issubclass(x, Attr)):

With the patch this could be greatly simplified to:

classes = (Element, ProcInst, Entity, CharRef, Attr)
if isinstance(x, type) and issubclass(x, classes):

(with classes being defined only once)
msg41890 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-11 21:34
Logged In: YES 
user_id=21627

Ok. The patch is fine, please apply it.
msg41891 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-12-12 16:47
Logged In: YES 
user_id=89016

Checked in as:
Doc/api/abstract.tex 1.22
Doc/lib/libfuncs.tex 1.123
Lib/test/test_isinstance.py 1.5
Misc/NEWS 1.556
Objects/abstract.c 2.111
Objects/classobject.c 2.165
Python/bltinmodule.c 2.267
msg41892 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-12-12 19:17
Logged In: YES 
user_id=89016

Updated to support recursive tuples:
Lib/test/test_isinstance.py 1.6
Objects/abstract.c 2.112
Objects/classobject.c 2.166
History
Date User Action Args
2022-04-10 16:05:58adminsetgithub: 37575
2002-12-06 16:32:04doerwaltercreate