+ 2

Built In Exceptions in Python

In this article : https://www.geeksforgeeks.org/built-exceptions-JUMP_LINK__&&__python__&&__JUMP_LINK/ it is mentioned that using " locals()['__builtins__'] " we can view all built in exceptions but I am getting only this output : <module 'builtins' (built-in)>

11th Jun 2019, 2:16 PM
harshit
harshit - avatar
7 Respuestas
+ 3
#Try this: print(list(filter(lambda x: x.lower() != x, dir(__builtins__))))
11th Jun 2019, 4:16 PM
Seb TheS
Seb TheS - avatar
+ 13
If you want to read about a module, you can use help() It will return a long content, which may give you a Time Limit on Sololearn, but if you try it on your ide, you'll have a nice detailed description. print(help(locals()['__builtins__']))
11th Jun 2019, 2:37 PM
Cépagrave
Cépagrave - avatar
+ 3
harshit Actually I am not sure what it is trying to mean with it. I did not get it work to return a list of Exceptions. Instead it returned the __builtins__ module, like you may have already discovered. Anyways you'll get a list of the errors with other builtin functions by printing dir(__builtins__).
11th Jun 2019, 7:58 PM
Seb TheS
Seb TheS - avatar
+ 2
Seb TheS can you explain how does the first one(your answer) functions to give all exceptions.
12th Jun 2019, 4:21 PM
harshit
harshit - avatar
+ 2
harshit Yes. __builtins__ is the namespace for all the builtin names (classes, functions and variables), that don't have any specific class. dir() returns all the names as list of strings from a certain namespace. lambda x: x.lower() != x is a function definition. When called, it expects x as string, and tests if lower case x is equal to original x. Warnings and errors are all capitalized, unlike the other names. It returns True, if x includes any upper case characters, otherwise it returns False. filter calls the function for each values of the list and if the lambda function returns True, the value is included in a new raw list type, and if it returns False, the value is ignored. Then only capitalized errors and warnings were included. Because the list type returned by filter cant be really used like it is, it is converted to a list and then printed.
12th Jun 2019, 7:10 PM
Seb TheS
Seb TheS - avatar
+ 1
You can type __builtins__. press TAB and you will see all the builtins of python.
11th Jun 2019, 2:34 PM
Almir
Almir - avatar
+ 1
Seb TheS is the method mentioned in the link is wrong ?
11th Jun 2019, 7:23 PM
harshit
harshit - avatar