在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/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'.
    """

其中一個(gè)函數(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 ""
        
這個(gè)函數(shù)到底返回了什么?難道返回了一個(gè)空字符嗎?

    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

這個(gè)函數(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
這個(gè)返回一個(gè)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

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

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

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

回答
編輯回答
別硬撐

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

另外,聽(tīng)樓上 “此用戶無(wú)昵稱” 的意思是說(shuō)看不到是因?yàn)樵创a被加密保護(hù),這種觀點(diǎn)是不對(duì)的,cpython的代碼是開(kāi)源的。

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

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

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

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

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

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

2018年8月14日 21:43
編輯回答
過(guò)客

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

2018年1月24日 02:58