`list()`被认为是一个函数吗?

Ayx*_*xan 5 python function list

list显然 Python中的内置类型.我在这个问题下看到了一个注释,它引用list()了一个内置函数.当我们检查文档时,它确实包含在内置函数列表中,但文档再次指出:

列表实际上是一种可变序列类型,而不是一个函数

这让我list()想到了一个问题:被认为是一个功能?我们可以将其称为内置函数吗?

如果我们谈论C++,我会说我们只是调用构造函数,但我不确定该术语是否constructor适用于Python(从未在此上下文中使用过它).

Dee*_*ace 11

list是一个type,这意味着它被定义为一个类,就像intfloat.

>> type(list)
<class 'type'>
Run Code Online (Sandbox Code Playgroud)

如果你检查它的定义builtins.py(实际代码用C实现):

class list(object):
    """
    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list.
    The argument must be an iterable if specified.
    """

    ...

    def __init__(self, seq=()): # known special case of list.__init__
        """
        Built-in mutable sequence.

        If no argument is given, the constructor creates a new empty list.
        The argument must be an iterable if specified.
        # (copied from class doc)
        """
        pass
Run Code Online (Sandbox Code Playgroud)

所以,list()不是一个功能.它只是调用list.__init__()(有一些与本讨论无关的参数)就像任何调用一样CustomClass().

感谢@jpg在评论中添加:Python中的类和函数有一个共同的属性:它们都被认为是callables,这意味着它们可以被调用().有一个内置函数callable可以检查给定的参数是否可调:

>> callable(1)
False
>> callable(int)
True
>> callable(list)
True
>> callable(callable)
True
Run Code Online (Sandbox Code Playgroud)

callable也定义于builtins.py:

def callable(i_e_, some_kind_of_function): # real signature unknown; restored from __doc__
    """
    Return whether the object is callable (i.e., some kind of function).

    Note that classes are callable, as are instances of classes with a
    __call__() method.
    """
    pass
Run Code Online (Sandbox Code Playgroud)