Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See class methods in Python console

Tags:

python

If I'm dealing with an object in the Python console, is there a way to see what methods are available for that class?

like image 866
Jason Swett Avatar asked Feb 01 '11 13:02

Jason Swett


People also ask

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 see the methods of a Python object?

You can use the built in dir() function to get a list of all the attributes a module has. Try this at the command line to see how it works. Also, you can use the hasattr(module_name, "attr_name") function to find out if a module has a specific attribute.

How do you print a class method in Python?

Print an Object in Python Using the __str__() Method Now, let's define the __str__() method of our example class ClassA and then try to print the object of the classA using the print() function. The print() function should return the output of the __str__() method.

What is __ method __ in Python?

__call__ method is used to use the object as a method. __iter__ method is used to generate generator objects using the object.


1 Answers

If by class, you actually meant the instance you have, you can simply use dir:

a = list()
print dir(a)

If you really meant to see the methods of the class of your object:

a = list()
print dir(a.__class__)

Note that in that case, both would print the same results, but python being quite dynamic, you can imagine attaching new methods to an instance, without it being reflected in the class.

If you are learning python and want to benefit from its reflection capabilities in a nice environment, I advise you to take a look at ipython. Inside ipython, you get tab-completion on methods/attributes

like image 74
David Cournapeau Avatar answered Sep 26 '22 17:09

David Cournapeau