例如:
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'.
"""
其中一个函数
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 ""
这个函数到底返回了什么?难道返回了一个空字符吗?
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
这个函数,返回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结尾的,那么要这个函数有什么意义
但是我在实际应用他们的时候,返回的并不是这样的?
我现在的以为是:这个类里面的这些函数,函数体里面什么逻辑都没有写?那么函数是怎么运行的?
4 回答
![?](http://img1.sycdn.imooc.com/533e4d660001312002000200-100-100.jpg)
白衣染霜花
TA贡献1796条经验 获得超10个赞
有些附带注释的接口是提供给用户看的,告诉用户如何使用,而源码是被加密保护的,至于保护的手段,不同的人各有不同。除非完全开源,否则有的函数的实现代码你是看不到的,你最多看到一些接口说明。
![?](http://img1.sycdn.imooc.com/545863aa00014aa802200220-100-100.jpg)
慕勒3428872
TA贡献1848条经验 获得超6个赞
python是C语言实现的,尽管有很多标准库是由python代码实现,但是涉及到底层支撑架构的功能还是C代码。一些IDE为了对这些进行友好代码提示,会弄和底层一样的访问接口,而其实现直接写 pass
略过。
另外,听楼上 “此用户无昵称” 的意思是说看不到是因为源码被加密保护,这种观点是不对的,cpython的代码是开源的。
![?](http://img1.sycdn.imooc.com/545865da00012e6402200220-100-100.jpg)
蓝山帝景
TA贡献1843条经验 获得超7个赞
底层是用c语言实现的,所以代码并没有真正的调用你贴的这些源码。
但是这些源码是非常有用的,因为当你help(str)
的时候,他们会显示出来。目的就是每个函数是做什么的,通过注释反射实现文档的一种方式。
比如下面定义的函数
def func(a: int, b: str):
return b * a
int和str并没有任何作用,但是当你用inspect.getfullargspec(func).annotations
的时候能看到每个变量的定义一样,当然定义除了可以是类,还可以是函数,常量等。
添加回答
举报
0/150
提交
取消