![]() |
How to Sorted and display the Subclasses of BaseException - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to Sorted and display the Subclasses of BaseException (/thread-32424.html) |
How to Sorted and display the Subclasses of BaseException - Fernando_7obink - Feb-09-2021 I have a task from the online course that i enrolled free recently ![]() def printExcTree(thisclass, nest = 0): if nest > 1: print(" |" * (nest - 1), end="") if nest > 0: print(" +---", end="") print(thisclass.__name__) for subclass in thisclass.__subclasses__(): printExcTree(subclass, nest + 1) printExcTree(BaseException)and here is the output : what i want to do is to sort every subclass depend on his first letter alphabetly and the subclass from every subclass follow the same rule...
RE: How to Sorted and display the Subclasses of BaseException - buran - Feb-09-2021 So, what have you tried? Because, what's the point if you don't try for yourself. RE: How to Sorted and display the Subclasses of BaseException - Gribouillis - Feb-09-2021 Try this perhaps for subclass in sorted(thisclass.__subclasses__()):Also have a look at module asciitree in pypi. RE: How to Sorted and display the Subclasses of BaseException - Fernando_7obink - Feb-09-2021 (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 ![]() ![]() RE: How to Sorted and display the Subclasses of BaseException - buran - Feb-09-2021 (Feb-09-2021, 07:44 AM)Fernando_7obink Wrote: it cant work because the sorted() only work on string or number typedo you know that sorted() takes optional key argument? it takes a function that is applied to each element and result is used for the sort.
RE: How to Sorted and display the Subclasses of BaseException - Fernando_7obink - Feb-09-2021 (Feb-09-2021, 07:57 AM)buran Wrote:Do you mean like this ?(Feb-09-2021, 07:44 AM)Fernando_7obink Wrote: it cant work because the sorted() only work on string or number typedo you know that a = ("h", "b", "a", "c", "f", "d", "e", "g") x = sorted(a, reverse=True) print(x) RE: How to Sorted and display the Subclasses of BaseException - buran - Feb-09-2021 No, not reverse , but key for subclass in sorted(thisclass.__subclasses__(), key=str): from the docs Quote:key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, RE: How to Sorted and display the Subclasses of BaseException - Gribouillis - Feb-09-2021 You could also try key = lambda klass: klass.__name__
RE: How to Sorted and display the Subclasses of BaseException - Fernando_7obink - Feb-10-2021 (Feb-09-2021, 08:52 AM)buran Wrote: No, notat last i can get a right answer, thank you.... can i ask more ... about the 'str' in key ... how does it work ? does it mean the key = str will take each element and change them to str type data and compare them ? because point to my case, the thisclass.__subclasses__()will give an output list, for each element is a 'class type' RE: How to Sorted and display the Subclasses of BaseException - buran - Feb-10-2021 (Feb-10-2021, 11:26 AM)Fernando_7obink Wrote: about the 'str' in key ... how does it work ? does it mean the key = str will take each element and change them to str type data and compare them ?yes, I think it's pretty clear from the docs I quoted in my answer. It will apply function supplied as key on each element in the list and use the result to compare/order the element, in this case the function is str() - a built-in function, in the @Gribouillis example - a lambda function (that is anonymous function) which takes one argument klass (i.e. one element) and returns klass.__name__ attribute (which is also a string).
|