msg122736 - (view) |
Author: Ram Rachum (cool-RR) * |
Date: 2010-11-28 21:47 |
>>> import abc >>> class A(object, metaclass=abc.ABCMeta): ... pass >>> issubclass([], A) Traceback (most recent call last): File "<pyshell#2>", line 1, in issubclass([], A) File "c:\Python32\lib\abc.py", line 137, in __subclasscheck__ if subclass in cls._abc_cache: File "c:\Python32\lib\_weakrefset.py", line 69, in __contains__ return ref(item) in self.data TypeError: cannot create weak reference to 'list' object I should be able to check whether an object is a subclass of something without confirming it's a type first. I think this call should just return False. |
|
|
msg122737 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2010-11-28 21:58 |
I don't agree. "issubclass(1, list)" has always raised an exception. |
|
|
msg122738 - (view) |
Author: Ram Rachum (cool-RR) * |
Date: 2010-11-28 22:01 |
But do you think it's a good idea that `issubclass(1, list)` raises an exception? Why not simply return `False`? Do you think there's someone out there who's counting on `issubclass` to raise an exception? |
|
|
msg122739 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2010-11-28 22:16 |
issubclass is for relationship between classes. Did you consider isinstance() instead? |
|
|
msg122740 - (view) |
Author: Ram Rachum (cool-RR) * |
Date: 2010-11-28 22:22 |
Amaury, I am aware of what `issubclass` does and what `isinstance` does. I am not manually feeding `[]` into `issubclass`. I have an object which can be either a list, or a string, or a callable, or a type. And I want to check whether it's a sub-class of some base class. So I don't think I should be taking extra precautions before using `issubclass`: If my object is not a subclass of the given base class, I should just get `False`. |
|
|
msg122741 - (view) |
Author: Benjamin Peterson (benjamin.peterson) *  |
Date: 2010-11-28 22:31 |
Tell python-ideas about it. |
|
|
msg122743 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2010-11-28 22:42 |
FWIW (which isn't much I guess) it annoys me that I have to protect calls to issubclass with if isinstance(obj, type). |
|
|