簡體   English   中英

Python:我如何找到/檢查 function 接受哪種 arguments?

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

我試圖找出 function 接受什么樣的 arguments。 這是因為我通常不確定 arguments 和 function 甚至首先接受什么。 例如,考慮來自 package Plotly 的 function:

fig.update_xaxes(ticks="outside")

我希望能夠知道不同的 arguments ticks聲可能是什么,即ticks="inside"ticks=outside

理想情況下,output 將是ticks accepts arguments such as: inside, outside, etc...

我通常會把箭頭所指的部分弄錯,因為我不知道tickstickson甚至首先接受的內容,以及它們的作用。

箭頭指向我想要提取的參數類型

現在我正在使用檢查。 但是,這並不能告訴我我可以輸入為 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

始終建議使用在線文檔,但請記住,文檔並不總是直接從代碼生成,即使是自動生成的,它也可能包含錯誤或過時。

如果您使用的是Jupyter Notebook ,您可以通過運行以下命令獲得有關任何 object 的幫助:

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:

在此處輸入圖像描述

然后當您使用Figure實例時:

在此處輸入圖像描述

然后,當您單擊它或在突出顯示某個項目時選擇ENTER時,參數將插入到您的代碼中,如下所示:

在此處輸入圖像描述

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 對其定義。

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.

的情況下:

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

點擊Figure將打開_figure.py文件並顯示以下代碼:

在此處輸入圖像描述

我希望這有幫助。

plotly.graph_objects.Figure.update_xaxes()的特定情況下,您可以將plotly.graph_objects.layout.Xaxis的構造函數接受的任何內容作為關鍵字參數傳入,我們將更新文檔字符串和文檔以實現這一點更清晰。 請注意, plotly.graph_objects.layout.Xaxis在其構造函數中接受我們所謂的“魔術下划線”,這意味着對於像title這樣的嵌套屬性,您可以傳入title_font等,這些也沒有在該文檔字符串中明確列出。 這是擁有這樣一個動態 API 的缺點之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM