list 是一个type,这意味着它在某处被定义为一个类,就像int 和float。
>> type(list)
<class 'type'>
如果你在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
所以,list() 不是一个函数。它只是调用list.__init__()(带有一些与本次讨论无关的参数),就像对CustomClass() 的任何调用一样。
感谢@jpg 添加 cmets:Python 中的类和函数有一个共同的属性:它们都被视为 callables,这意味着它们可以被 () 调用。有一个内置函数 callable 可以检查给定参数是否可调用:
>> callable(1)
False
>> callable(int)
True
>> callable(list)
True
>> callable(callable)
True
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