鍍金池/ 問答/Python/ 查看python源碼,發(fā)現(xiàn)里面的函數(shù)都以pass結(jié)尾,那么意義何在?

查看python源碼,發(fā)現(xiàn)里面的函數(shù)都以pass結(jié)尾,那么意義何在?

例如:

class str(object):
    """
    str(object='') -> str
    str(bytes_or_buffer[, encoding[, errors]]) -> str
    
    Create a new string object from the given object. If encoding or
    errors is specified, then the object must expose a data buffer
    that will be decoded using the given encoding and error handler.
    Otherwise, returns the result of object.__str__() (if defined)
    or repr(object).
    encoding defaults to sys.getdefaultencoding().
    errors defaults to 'strict'.
    """

其中一個函數(shù)
    def capitalize(self): # real signature unknown; restored from __doc__
        """
        S.capitalize() -> str
        
        Return a capitalized version of S, i.e. make the first character
        have upper case and the rest lower case.
        """
        return ""
        
這個函數(shù)到底返回了什么?難道返回了一個空字符嗎?

    def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.endswith(suffix[, start[, end]]) -> bool
        
        Return True if S ends with the specified suffix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        suffix can also be a tuple of strings to try.
        """
        return False

這個函數(shù),返回False ,什么意思?
    def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0
這個返回一個0 ?

    def format(self, *args, **kwargs): # known special case of str.format
        """
        S.format(*args, **kwargs) -> str
        
        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
        """
        pass

這個是一pass結(jié)尾的,那么要這個函數(shù)有什么意義

但是我在實際應(yīng)用他們的時候,返回的并不是這樣的?

我現(xiàn)在的以為是:這個類里面的這些函數(shù),函數(shù)體里面什么邏輯都沒有寫?那么函數(shù)是怎么運行的?

回答
編輯回答
別硬撐

python是C語言實現(xiàn)的,盡管有很多標(biāo)準(zhǔn)庫是由python代碼實現(xiàn),但是涉及到底層支撐架構(gòu)的功能還是C代碼。一些IDE為了對這些進行友好代碼提示,會弄和底層一樣的訪問接口,而其實現(xiàn)直接寫 pass 略過。

另外,聽樓上 “此用戶無昵稱” 的意思是說看不到是因為源碼被加密保護,這種觀點是不對的,cpython的代碼是開源的。

2018年9月2日 01:51
編輯回答
不歸路

底層是用c語言實現(xiàn)的,所以代碼并沒有真正的調(diào)用你貼的這些源碼。
但是這些源碼是非常有用的,因為當(dāng)你help(str)的時候,他們會顯示出來。目的就是每個函數(shù)是做什么的,通過注釋反射實現(xiàn)文檔的一種方式。
比如下面定義的函數(shù)

def func(a: int, b: str):
    return b * a

int和str并沒有任何作用,但是當(dāng)你用inspect.getfullargspec(func).annotations的時候能看到每個變量的定義一樣,當(dāng)然定義除了可以是類,還可以是函數(shù),常量等。

2017年3月22日 16:23
編輯回答
病癮

有些附帶注釋的接口是提供給用戶看的,告訴用戶如何使用,而源碼是被加密保護的,至于保護的手段,不同的人各有不同。除非完全開源,否則有的函數(shù)的實現(xiàn)代碼你是看不到的,你最多看到一些接口說明。

2018年8月14日 21:43
編輯回答
過客

這個不是源碼、只是一個類似于“接口”的東西、只能從這里看到有哪些函數(shù)(方法)、都有些什么參數(shù)。

2018年1月24日 02:58