2012-09-22 20 views
1

我使用Python的dir()函數來確定哪些屬性和類有方法的方法。Python的DIR命令來確定

例如確定wx.Frame的方法,我用dir(wx.Frame)

是否有任何命令來確定每個方法的參數列表?例如,如果我想知道什麼參數屬於wx.Frame.CreateToolBar()

+2

你在說什麼*參數/參數*?否則,你的問題沒有多大意義。 – delnan

+1

嘗試使用'help'。 'help(wx.Frame.CreateToolBar)'例如,會告訴你需要調用方法的簽名 – GP89

+0

謝謝..這有助於.. – user1050619

回答

2

正如評論所說,你可以使用help(fun)輸入與函數的簽名與文檔字符串的幫助編輯器。您也可以簡單地使用print fun.__doc__,對於大多數成熟的庫,您應該獲得有關參數和函數簽名的合理文檔。

如果你談論的是交互式的幫助,可以考慮使用IPython其中有一些有用的附加功能。例如,您可以鍵入%psource fun以獲得功能fun的源代碼打印輸出,並且只需鍵入wx.Frame.並按Tab鍵即可查看wx.Frame中可用的所有方法和屬性的列表。

1

即使GP89似乎已​​經回答了這個問題,我想我會跳在更詳細一點。

首先,GP89的建議是使用Python的內置help() method。這是您可以在交互式控制檯中使用的方法。對於方法,它將打印方法的聲明行以及類'docstring(如果已定義)。您也可以通過<object>.__doc__訪問此例如:

>>> def testHelp(arg1, arg2=0): 
... """This is the docstring that will print when you 
... call help(testHelp). testHelp.__doc__ will also 
... return this string. Here is where you should 
... describe your method and all its arguments.""" 
... 
>>> help(testHelp) 
Help on function testHelp in module __main__: 

testHelp(arg1, arg2=0) 
    This is the docstring that will print when you 
    call help(testHelp). testHelp.__doc__ will also 
    return this string. Here is where you should 
    describe your method and all its arguments. 
>>> 

然而,對於理解方法,類和函數的另一個非常重要的工具就是工具包的API。對於內置的Python函數,您應該檢查Python Doc Library。這就是我找到help()函數的文檔的地方。您使用wxPython的,它的API可以發現here,所以對於「wx.Frame API」快速搜索,你可以找到this page describing all of wx.Frame's methods and variables.不幸的是,CreatteToolBar()是不是特別有據可查的,但你仍然可以看到它的參數:

CreateToolBar(self,style,winid,name)

快樂編碼!