Python Forum
How to Sorted and display the Subclasses of BaseException
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Sorted and display the Subclasses of BaseException
#4
(Feb-09-2021, 06:42 AM)buran Wrote: So, what have you tried? Because, what's the point if you don't try for yourself.
i have done few things e.g.
try to sort the subclass first by sorting the for loop as follow
for subclass in sorted(thisclass.__subclasses__()):
, but it cant work because the sorted() only work on string or number type

then i try to create a list that will consist of the subclass name that already converted to "string" type, so next i can sort that list, then print it, so i try this code
def printExcTree(thisclass, nest = 0):
    if nest > 1:
        print("   |" * (nest - 1), end="")
    if nest > 0:
        print("   +---", end="")

    print(thisclass.__name__)
    
    a = []
    for subclass in thisclass.__subclasses__():
        a.append(subclass.__name__)
        for srt_subclass in a :
            printExcTree(srt_subclass, nest + 1)

printExcTree(BaseException)
and cant work because when it comes to recursion
printExcTree(srt_subclass, nest + 1)
the first parameter pass a str type, meanwhile the
print(thisclass.__name__)
only can work on class type. Is there any way to change the str type becoming a class type Big Grin e.g. ValueError become "ValueError" then i can change it again "valueError" become ValueError Big Grin
Reply


Messages In This Thread
RE: How to Sorted and display the Subclasses of BaseException - by Fernando_7obink - Feb-09-2021, 07:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 17,374 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,115 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  defining a function to see if a list is sorted Siylo 14 7,258 Nov-29-2018, 05:25 PM
Last Post: wavic
  sorted list not displaying Crackity 6 5,216 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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