1

In java, if you search up a class, you can usually find all of the available methods that are provided from the class. It tells you the parameters and the return type. However, in python, I haven't been able to find anything like that. The official docs.python.org Has examples of some method uses but they sometimes they don't seem to mention exactly what the arguments it takes in are and what the return type is.

For example, when I was looking at the counter object, I was not able to find exactly what keys() method does and what it returns. Similarly, i saw another usage of the counter class with get but I was not able to find that on that website either.

Does anyone have any suggestions to documentation that is very similar to Java? Or if not, the best approach to figuring out what each method does in python.

2 Answers 2

3

Python has something called a "docstring", analagous to Javadoc. Most methods are written with docstrings (the standard library is pretty good about this, at least), and most IDEs will automatically show you the docstring for any given method you're about to reference, in the same way that they would show you the javadoc. Docstring looks like this:

def my_method(some_params):
    '''
    docstring goes here, within the triple-quotes
    '''

When you're on a python terminal, you can look directly at the docstring for a given method by doing

>>> help(my_method)

You can also import a package and look at a particular method:

>>> import collections
>>> help(collections.Counter.keys)

This then shows you the following:

keys(...)
    D.keys() -> a set-like object providing a view on D's keys

You can also use help() on a class, which will show the class's docstring and the docstrings for each method in the class:

>>> help(collections.Counter)

or on an instance, which would show more or less the same information:

>>> x = collections.Counter()
>>> help(x)

In your particular case, Counter is a subclass of the built-in dict, which might account for the lack of explicit documentation on the methods they share. dict.get(key), for example, returns the value for the given key, so I'd assume Counter.get(key) would return something similar.


If looking at the docstring doesn't really help you figure out your issue, don't be afraid to just open up a terminal and experiment with the inputs and outputs!

Sign up to request clarification or add additional context in comments.

Comments

0

We can use either of the below code to get the required details.

from collections import Counter
help(Counter)
from collections import Counter
Counter.__doc__

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.