Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find python's built-in classes' methods and attributes? [closed]

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 :(

like image 240
João Josézinho Avatar asked Sep 04 '12 15:09

João Josézinho


People also ask

How can I find the methods or attributes of an object in Python?

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.

Where are methods stored in Python?

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.

How do I see the methods of a class in Python?

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.

How do I find the built-in function in Python?

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() .


2 Answers

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).

like image 156
Kevin Avatar answered Oct 12 '22 10:10

Kevin


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() or unicode() 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.]

like image 22
Fred Foo Avatar answered Oct 12 '22 11:10

Fred Foo