我第一次启动python shell时有以下名称是什么?它们看起来不像以下功能__builtins__:
>>> dir(__name__)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
Run Code Online (Sandbox Code Playgroud)
__name__是一个字符串和那些字符串方法.它是模块的名称或'__main__'在顶层.因此,这个成语经常出现:
if __name__ == '__main__':
# this file was called directly, not imported
main()
Run Code Online (Sandbox Code Playgroud)
dir(__name__)显示属性__name__,并且由于__name__是字符串,它显示str类的属性.大多数列出的属性都是方法.您可以使用help()以下方式获取更多信
>>> help(str.index)
Help on method_descriptor:
index(...)
S.index(sub [,start [,end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
Run Code Online (Sandbox Code Playgroud)