鍍金池/ 問(wèn)答/Python  Linux/ 請(qǐng)教一個(gè)py關(guān)鍵字參數(shù)問(wèn)題

請(qǐng)教一個(gè)py關(guān)鍵字參數(shù)問(wèn)題

ss = '012345.78'
print(ss.endswith('.', 1, 7))

結(jié)果是True,沒(méi)有疑問(wèn)~~

但是我無(wú)意中用了關(guān)鍵字參數(shù)傳遞
ss.endswith('.', start=1, end=7)
ss.endswith('.', 1, end=7)

**沒(méi)想到都拋異常
TypeError: endswith() takes no keyword arguments**

請(qǐng)問(wèn)是什么原因?

回答
編輯回答
毀憶

本來(lái) endswith() 就沒(méi)有 startend 參數(shù),它的函數(shù)原型是這樣的

Docstring:
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.
Type:      builtin_function_or_method

只有函數(shù)原型是
endswith(suffix, start=0, end=-1)
或者
endswith(suffix, **kwargs)
時(shí),才可以使用 endswith(xx, start=N, end=M) 方式調(diào)用。

2018年3月14日 04:55