2015-12-19 17 views
5

Có nhiều phương pháp Python được xây dựng sẵn mà tôi muốn nghiên cứu mã nguồn để hiểu. Làm thế nào tôi có thể tìm thấy vị trí của họ trên máy tính của tôi? Có một số lệnh đơn giản mà tôi có thể chạy hoặc trong một kịch bản Python hoặc trong thiết bị đầu cuối của tôi trên Linux của tôi mà tôi có thể sử dụng để xác định vị trí tệp nguồn của phương thức dựng sẵn?Làm thế nào tôi có thể tìm thấy vị trí của mã nguồn của một phương thức Python tích hợp?

+3

Bạn có thể quan tâm đến http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions –

Trả lời

4

Bạn thường có thể tìm thấy tệp nguồn cho các mô-đun python lõi trong thư mục cài đặt python. Ví dụ, trên linux, tôi có thể tìm ra mã nguồn cho os mô-đun mà là một mô-đun python khá phổ biến ở vị trí này:

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

Nếu bạn đang ở trên windows, điều này thường là C:\python27\lib, nhưng bạn có thể xác minh nó cho chính bạn bằng cách chạy which python trong trường hợp của linux và where python trong trường hợp windows.

+0

Ví dụ của tôi là phương thức chuỗi built-in isspace(). Điều đó không yêu cầu nhập bất kỳ mô-đun lõi bổ sung nào. Tôi có thể tìm nguồn cho điều đó ở đâu? – Rohan

+1

Các hàm dựng sẵn và các hàm mức thấp khác được triển khai trong 'C' vì các lý do rõ ràng về hiệu suất, do đó chúng chỉ có sẵn ở dạng biên dịch bit. Tuy nhiên, bạn vẫn có thể thấy mã nguồn cho các hàm này bằng cách truy cập [mã nguồn python] (https://hg.python.org/cpython/file/c6880edaf6f3). Câu trả lời khác này là [để tham khảo.] (Http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions) –

+0

Đặc biệt, trong '/ Objects/stringobject. c', [ở đây] (https://hg.python.org/cpython/file/c6880edaf6f3/Objects/stringobject.c), bạn có thể tìm thấy hàm, 'string_isspace()' - cái bạn đang tìm kiếm. –

2

Để có được vị trí tập tin của Python từ nhà ga:

$ which python 

Nhưng bạn có thể xem mã nguồn của một hàm đơn giản bằng cách thêm nó với ?? (lưu ý rằng một số chức năng biên dịch C và không được viết bằng Python).

Ví dụ:

# 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 

    ... 
Các vấn đề liên quan