list
is a type
,这意味着它被定义为一个类,就像int
and 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
So, list()
不是一个函数。它只是在呼唤list.__init__()
(有些参数与本讨论无关)就像任何调用一样CustomClass()
是在做。
感谢@jpg添加评论:Python中的类和函数有一个共同的属性:它们都被视为可调用对象,这意味着它们可以被调用()
。有一个内置函数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