Dee*_*ace 11
list是一个type,这意味着它被定义为一个类,就像int和float.
>> 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)