简体   繁体   中英

Python: How do I find / inspect what kind of arguments a function accepts?

I am trying to find out what what kind of arguments a function accepts. This is because I usually am unsure of what arguments a function even accepts in a first place. For example, consider a function from the the package Plotly:

fig.update_xaxes(ticks="outside")

I want to be able to know what are the different arguments ticks could be, ie ticks="inside" or ticks=outside .

Ideally the output would be that ticks accepts arguments such as: inside, outside, etc...

I usually get the parts pointed out by the arrows wrong because I don't know what ticks and tickson even accepts in the first place, as well as, what they do.

箭头指向我想要提取的参数类型

Right now I am using inspect. But, this doesn't tell me that I can input as arguments.

>>import inspect
>>inspect.getfullargspec(go.Figure.update_xaxes)
>>print(inspect.getsource(go.Figure.update_xaxes))
OUTPUT:
    def update_xaxes(self, patch=None, selector=None, row=None, col=None, **kwargs):
        """
        Perform a property update operation on all xaxis objects
        that satisfy the specified selection criteria

        Parameters
        ----------
        patch: dict
            Dictionary of property updates to be applied to all
            xaxis objects that satisfy the selection criteria.
        selector: dict or None (default None)
            Dict to use as selection criteria.
            xaxis objects will be selected if they contain
            properties corresponding to all of the dictionary's keys, with
            values that exactly match the supplied values. If None
            (the default), all xaxis objects are selected.
        row, col: int or None (default None)
            Subplot row and column index of xaxis objects to select.
            To select xaxis objects by row and column, the Figure
            must have been created using plotly.subplots.make_subplots.
            If None (the default), all xaxis objects are selected.
        **kwargs
            Additional property updates to apply to each selected
            xaxis object. If a property is specified in
            both patch and in **kwargs then the one in **kwargs
            takes precedence.
        Returns
        -------
        self
            Returns the Figure object that the method was called on
        """
        for obj in self.select_xaxes(selector=selector, row=row, col=col):
            obj.update(patch, **kwargs)

        return self

Using online documentation is always recommended but please keep in mind that documentation is not always generated directly from code or even if auto-generated it can contain errors or be out of date.

If you are using Jupyter Notebook you can get help on any object by running:

help(object)

If you are using an IDE like Eclipse the object options (parameters, etc..) are usually displayed to you as you type in your IDE:

在此处输入图像描述

And then when you are using a Figure instance:

在此处输入图像描述

Then when you click on it or choose ENTER when highlighting an item the parameters are inserted into your code like this:

在此处输入图像描述

In most IDE's you can also push CMD (Mac) or CTRL (Windows) when hovering over the library, function, object or a variable in your code (text changes into a link and the cursor changes to hand ) and then click on it to go to its definition.

If the object or function is defined in another file, that file will automatically open in your IDE and the cursor will point to that object/function that you clicked.

In case of:

import plotly.graph_objects as go
fig = go.Figure(data, layout)

clicking on Figure will open _figure.py file and display this code:

在此处输入图像描述

I hope this helps.

In the specific case of plotly.graph_objects.Figure.update_xaxes() , you can pass in as a keyword argument anything that is accepted by the constructor of plotly.graph_objects.layout.Xaxis , and we will update the docstring and documentation to make that clearer. Note that plotly.graph_objects.layout.Xaxis accepts in its constructor what we call "magic underscores" meaning that for nested properties like title you can pass in title_font and so on, which are also not explicitly listed in that docstring. This is one of the downsides to having such a dynamic API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM