2016-08-30 4 views
0

私はOpenCVを使用しています。「矩形」機能が何であるかを見たいと思います。 dir(module)関数を使用して関数定義と名前を取得できますが、実際の関数の表示方法はわかりません。私はLinux(Ubuntu 16.04)を使用していますが、ライブラリが "/ usr/local /"か他の場所にあるかどうかは疑問です。 OpenCVのcv2のpythonライブラリは単なる例です。私はPythonにインポートされたライブラリの関数を表示する方法を知りたいと思います。Pythonモジュールの機能を見つけるにはどうすればいいですか?

+1

https://docs.python.org/3/library/inspect.html#inspect.getsourcelines –

答えて

3

は、複数の方法があります:あなたはinspect.getsourcelines(object)を使用することができ、実行時にソースコードを取得したい場合は

0

私はあなたが素晴らしいIPython対話型シェルを使用することをお勧めします。

名前に2つの疑問符(??)を追加することで、関数の定義(またはソースコードが使用可能なオブジェクトの一般的なもの)を見ることができます。以下は私の端末で実行される短い例です:

$ ipython 
Python 2.7.9 (default, Jan 27 2016, 11:42:08) 
Type "copyright", "credits" or "license" for more information. 

IPython 2.3.1 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 

In [1]: import urllib 

In [2]: urllib.urlopen?? 
Type:  function 
String form: <function urlopen at 0x2b56465ac578> 
File:  /sw/python/2.7.9/Linux.x86_64/lib/python2.7/urllib.py 
Definition: urllib.urlopen(url, data=None, proxies=None) 
Source: 
def urlopen(url, data=None, proxies=None): 
    """Create a file-like object for the specified URL to read from.""" 
    from warnings import warnpy3k 
    warnpy3k("urllib.urlopen() has been removed in Python 3.0 in " 
      "favor of urllib2.urlopen()", stacklevel=2) 

    global _urlopener 
    if proxies is not None: 
     opener = FancyURLopener(proxies=proxies) 
    elif not _urlopener: 
     opener = FancyURLopener() 
     _urlopener = opener 
    else: 
     opener = _urlopener 
    if data is None: 
     return opener.open(url) 
    else: 
     return opener.open(url, data) 
関連する問題