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 за добавление в комментарии: классов и функций в 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