If I'm dealing with an object in the Python console, is there a way to see what methods are available for that class?
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.
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.
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.
__call__ method is used to use the object as a method. __iter__ method is used to generate generator objects using the object.
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
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