I'm trying to find out which methods and attributes come with the mother of all exception classes in python: the Exception class. However, I'm having a bit of trouble since the official documentation doesn't seem to provide it.
The best I could find was this: http://docs.python.org/library/exceptions.html but that only lists the built-in exceptions.
What's going on? I'm used to the Java and PHP documentations where everything is laid down on the table :(
Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.
Memory Allocation in Python The methods/method calls and the references are stored in stack memory and all the values objects are stored in a private heap.
To list the methods for this class, one approach is to use the dir() function in Python. The dir() function will return all functions and properties of the class.
Check with the built-in function dir() The built-in function dir() returns a list of names of attributes, methods, etc. of the object specified in the argument. You can get a list of names of built-in objects, such as built-in functions and constants, by passing the builtins module or __builtins__ to dir() .
The built-in function dir
will give a list of names comprising the methods and attributes of an object.
>>>print dir(Exception)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribut
e__', '__getitem__', '__getslice__', '__hash__', '__init__', '__new__', '__reduc
e__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__',
'__str__', '__subclasshook__', '__unicode__', 'args', 'message']
You can also get help using the help
method: help(Exception)
.
There's only one interesting attribute on BaseException
, and that's args
. This is documented, so there's no problem.
There are no methods on BaseException
other than the special (__
) methods, which you should not call directly. Of these, __str__
is documented by the sentence
If
str()
orunicode()
is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments.
[There is one more public attribute, message
, but if you access that you will get a DeprecationWarning
. Deprecated attributes are not always documented since you shouldn't use them in new code.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With