Issue 17157: issubclass() should accept iterables in 2nd arg (original) (raw)

Created on 2013-02-08 14:06 by Ramchandra Apte, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)

msg181670 - (view)

Author: Ramchandra Apte (Ramchandra Apte) *

Date: 2013-02-08 14:07

kushou pointed this out on #python-dev

msg181672 - (view)

Author: Mark Dickinson (mark.dickinson) * (Python committer)

Date: 2013-02-08 14:55

What's the use case for this? issubclass already accept tuples, just like isinstance:

issubclass(bool, (int, float)) True

msg181714 - (view)

Author: Terry J. Reedy (terry.reedy) * (Python committer)

Date: 2013-02-09 02:59

Given isxxx(src, target_s), the proposal would seem to be to change the internal test "type(target_s) is tuple" to "hasattr(type(target_s), 'iter'). This depends on metaclasses not having .iter methods, just as type does not. However, a subclass of type that added .iter, for instance to iterate through all its instances (classes defined with that metaclass), would break that test. So the proposal needs a better test, that cannot become ambiguous, to be practical.

A virtue of the 'class or tuple of classes' interface is that a tuple instance is clearly not a class in itself, so there is no possible ambiguity.

It is a positive feature that isinstance and issubclass have the same signature: both or neither should be changed. The use of tuple for multiple items in 'item or items' interfaces is historical and also used elsewhere, as in exception clauses.

The meaning of except target_exception_s [as name]: body is if issubclass(raised_exception, target_exception_s) or isinstance(raised_exception, target_exception_s): [name = raised_exception] body

So to remain consistent, I think changing exception statements to allow iterables of exceptions should also be part of the proposal.

There might be something else that I am forgetting about at the moment.

While iterables might be used if Python were being written fresh today and the ambiguity problem were solved, I agree that more than esthetic satisfaction is needed to make a change.

msg181730 - (view)

Author: Ramchandra Apte (Ramchandra Apte) *

Date: 2013-02-09 10:35

Just so you know, I'm neutral on this idea. I think it should at least accept sequences though.

msg181737 - (view)

Author: Franck Michea (franck) *

Date: 2013-02-09 13:49

I am neutral on this too, it just felt odd and this is why the question raised.

Yesterday I tried to add sequences and iterables (only to issubclass but indeed it would need better testing and all) and came to a problem with sequences. The current implementation authorizes (A, (B, (C, D))) (why?) so issubclass is recursive, but if we add sequences, they could create infinite recursions if seq[0] == seq (it's the case for strings for example, where 'a' is still a sequence and 'a'[0] == 'a').

So I don't know if it's really interesting. Anyone can use tuple() on an iterable to build its tuple value, though the value is built in memory. It basically just felt odd to take only tuples but I don't know.

msg381189 - (view)

Author: Irit Katriel (iritkatriel) * (Python committer)

Date: 2020-11-16 23:51

Good discussion. There seems to be agreement that this should not be done.

msg381209 - (view)

Author: Terry J. Reedy (terry.reedy) * (Python committer)

Date: 2020-11-17 06:34

except ... is another instance of (exception) class or tuple of classes.

History

Date

User

Action

Args

2022-04-11 14:57:41

admin

set

github: 61359

2020-11-17 06:34:31

terry.reedy

set

messages: +

2020-11-16 23:51:59

iritkatriel

set

status: open -> closed

nosy: + iritkatriel
messages: +

resolution: rejected
stage: resolved

2013-02-09 13:49:44

franck

set

messages: +

2013-02-09 10:35:05

Ramchandra Apte

set

messages: +

2013-02-09 02:59:24

terry.reedy

set

nosy: + terry.reedy
messages: +

2013-02-08 14:55:26

mark.dickinson

set

nosy: + mark.dickinson

messages: +
versions: - Python 3.3

2013-02-08 14:12:24

Ramchandra Apte

set

status: languishing -> open

2013-02-08 14:10:02

Ramchandra Apte

set

status: open -> languishing

2013-02-08 14:08:02

Ramchandra Apte

set

title: issubclass should accept iterables -> issubclass() should accept iterables in 2nd arg

2013-02-08 14:07:33

Ramchandra Apte

set

messages: +

2013-02-08 14:06:55

Ramchandra Apte

set

components: + Interpreter Core

2013-02-08 14:06:31

Ramchandra Apte

create