2015-12-19 11 views
5

理解しやすいソースコードを学習したいPythonの組み込みのメソッドがたくさんあります。自分のコンピュータ上で自分の位置を見つけるにはどうすればいいですか?私がPythonのスクリプトやビルドインメソッドのソースファイルを見つけるために私のLinux上のターミナルで実行できる簡単なコマンドはありますか?組み込みのPythonメソッドのソースコードの場所を知るにはどうすればいいですか?

+3

あなたはhttp://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functionsに興味がある可能性があり –

答えて

4

core pythonモジュールのソースファイルは、通常、pythonインストールフォルダ自体にあります。例えば、linuxに、私はこの場所では非常に人気のpythonモジュールですosモジュールのソースコードを見つけることができます。

/usr/lib/python2.7/os.py 

あなたがwindows上にある場合、これは一般的にC:\python27\libですが、あなたがそれを確認することができますlinuxの場合はwhich pythonwindowsの場合はwhere pythonを実行してください。

+0

私の例組み込みの文字列メソッドisspace()です。これは、追加のコアモジュールのインポートを必要としません。どこでそのソースを見つけることができますか? – Rohan

+1

組み込み関数やその他の低レベル関数は、パフォーマンスの明白な理由から 'C'で実装されているため、ビットコンパイルされた形式でしか利用できません。しかし、[python source repo](https://hg.python.org/cpython/file/c6880edaf6f3)にアクセスすると、これらの関数のソースコードが引き続き表示されます。この他の答えは[参考用です。](http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions) –

+0

特に、/ Objects/stringオブジェクトにあります。 c '、[ここ](https://hg.python.org/cpython/file/c6880edaf6f3/Objects/stringobject.c)には、あなたが探している関数 'string_isspace()'があります。 –

2

端末からのPythonのファイルの場所を取得するには:

$ which python 

しかし、あなたは(いくつかの機能は、Cコンパイルとで書かれていないことに注意して単に??でそれを追加することによって、機能のソースコードを見ることができますPython)。例えば

# Example 1: Built in compiled function. 
>>> open?? 
Docstring: 
open(name[, mode[, buffering]]) -> file object 

Open a file using the file() type, returns a file object. This is the 
preferred way to open a file. See file.__doc__ for further information. 
Type:  builtin_function_or_method 

# Example 2: Pandas function written in Python. 
import pandas as pd 
>>> pd.DataFrame?? 

Init signature: pd.DataFrame(self, data=None, index=None, columns=None, dtype=None, copy=False) 
Source: 
class DataFrame(NDFrame): 

    """ Two-dimensional size-mutable, potentially heterogeneous tabular data 
    structure with labeled axes (rows and columns). Arithmetic operations 
    align on both row and column labels. Can be thought of as a dict-like 
    container for Series objects. The primary pandas data structure 

    Parameters 
    ---------- 
    data : numpy ndarray (structured or homogeneous), dict, or DataFrame 
     Dict can contain Series, arrays, constants, or list-like objects 
    index : Index or array-like 
     Index to use for resulting frame. Will default to np.arange(n) if 
     no indexing information part of input data and no index provided 
    columns : Index or array-like 
     Column labels to use for resulting frame. Will default to 
     np.arange(n) if no column labels are provided 
    dtype : dtype, default None 
     Data type to force, otherwise infer 
    copy : boolean, default False 
     Copy data from inputs. Only affects DataFrame/2d ndarray input 

    Examples 
    -------- 
    >>> d = {'col1': ts1, 'col2': ts2} 
    >>> df = DataFrame(data=d, index=index) 
    >>> df2 = DataFrame(np.random.randn(10, 5)) 
    >>> df3 = DataFrame(np.random.randn(10, 5), 
    ...     columns=['a', 'b', 'c', 'd', 'e']) 

    See also 
    -------- 
    DataFrame.from_records : constructor from tuples, also record arrays 
    DataFrame.from_dict : from dicts of Series, arrays, or dicts 
    DataFrame.from_items : from sequence of (key, value) pairs 
    pandas.read_csv, pandas.read_table, pandas.read_clipboard 
    """ 

    @property 
    def _constructor(self): 
     return DataFrame 

    _constructor_sliced = Series 

    @property 
    def _constructor_expanddim(self): 
     from pandas.core.panel import Panel 
     return Panel 

    ... 
関連する問題