Python Forum
issubclass() not as documented
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issubclass() not as documented
#1
documentation for builtin function issubclass() says it returns True for the right conditions but otherwise raises TypeError. when i call it with a wrong condition it just returns False.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Wouldn't it help to show the code you're trying? Otherwise, we're having to assume what you mean by "a wrong condition".
Reply
#3
This could be an opportunity for you to become a Python core developer by sending a pull request.
Reply
#4
(Mar-07-2021, 09:15 AM)Gribouillis Wrote: This could be an opportunity for you to become a Python core developer by sending a pull request.

i don't want to become a Python core developer.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
maybe, instead, i'll create Cobra, which if an exception is raised, deletes the source file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Mar-06-2021, 11:40 PM)Skaperen Wrote: documentation for builtin function issubclass() says it returns True for the right conditions but otherwise raises TypeError.
No,it say In any other case, a TypeError exception is raised.
Any other cases is when not return True or False.
class Animals:
    pass

class Humans(Animals):
    pass
>>> issubclass(Humans, Animals)
True
>>> issubclass(Animals, Humans)
False

>>> issubclass(Animals, 'foo')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: issubclass() arg 2 must be a class or tuple of classes
Reply
#7
where does it say the part "when not return True or False"? where does is say anything about False?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Given that the function returns True in some cases, it should be obvious that it would return False in others (and really if a function is called "is something", you'd expect it to return a Boolean. Throwing an exception in the false case seems like bad design to me, as it's confusing for the caller.
Reply
#9
Quote:classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.
What it's saying is that it raises TypeError when you pass it something other than a tuple.
It might not be perfectly clear from that that a single class is also allowed, but it doesn't say an exception is raised unless the condition is true.
Reply
#10
(Mar-08-2021, 06:28 AM)ndc85430 Wrote: Given that the function returns True in some cases, it should be obvious that it would return False in others (and really if a function is called "is something", you'd expect it to return a Boolean. Throwing an exception in the false case seems like bad design to me, as it's confusing for the caller.

it may seem obvious but lots of other things are documented complete enough that you don't need to depend on your imagination. besides this documentation does say what it does in other cases.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  where is this documented? Skaperen 8 4,570 Feb-20-2018, 03:31 AM
Last Post: Skaperen
  format spec %b is documented but does not work Skaperen 13 10,471 Jul-31-2017, 09:21 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020