将标准库提取到方法文件

时间:2017-06-18 11:30:23

标签: python

我需要为用其他语言编写的自定义编辑器创建自动完成文件。关键字只是:

>>> import keyword
>>> s = " ".join(keyword.kwlist) 

但我不知道是否有类似的东西。如果没有,Python团队会为其文档维护任何可解析的格式吗?

我需要的只是name函数及其可能的参数以及它实际执行的内容的描述。像package name et al这样的其他细节也很好。

例如Python有open function,因此它将是:

名称open()

签名open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

说明Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised....

我希望很清楚。让我知道任何仍然含糊不清的内容。

2 个答案:

答案 0 :(得分:1)

在python3中,任何对象都有属性__name__(和__qualname__,不确定区别是什么),__text_signature____doc__

答案 1 :(得分:1)

根据您的问题,您希望在变量上显示一些信息。 python中的对象包含一些内置属性,您可以阅读这些属性来获取此信息。

  1. __name__:提供对象的名称(注意,如果对象没有此对象,则会引发AttributeError

    >>> f = 5
    >>> f.__name__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'int' object has no attribute '__name__'
    
  2. __doc__:提供有关对象的有用帮助文本。可能包含也可能不包含任何内容。

    >>> print(f.__doc__)
    int(x=0) -> integer
    int(x, base=10) -> integer
    
    Convert a number or string to an integer, or return 0 if no arguments
    are given.  If x is a number, return x.__int__().  For floating point
    numbers, this truncates towards zero.
    
    If x is not a number or if base is given, then x must be a string,
    bytes, or bytearray instance representing an integer literal in the
    given base.  The literal can be preceded by '+' or '-' and be surrounded
    by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
    Base 0 means to interpret the base from the string as an integer literal.
    >>> int('0b100', base=0)
    4
    
  3. dir(__builtins__):检索有关所有内置的信息。

    >>> dir(__builtins__)
    ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']