about 'setattr(o, name, value)' and 'inspect.signature(f)'

Thomas Jollans tjol at tjol.eu
Tue Oct 10 12:14:22 EDT 2017


On 2017-10-10 16:37, xieyuheng wrote:
>    > Some callables may not be introspectable in certain implementations of Python.
>    > For example, in CPython, some built-in functions defined in C
>    > provide no metadata about their arguments.
> 
>    this is depends on implementation, so I ask for CPython.

There's not really much more to say than what the docs you're quoting say.

As far as I'm aware, all callables defined in Python will have a
signature, while the default for "built-in" functions (anything written
in C) in CPython is for them not to have a signature. This applies to
__builtins__ and other modules written in C equally.

In __builtins__, most callables have NO signature as of Python 3.6.
However, around 3/4 of the *functions* there (i.e. not types) DO have
signatures. In particular none of the built-in exceptions have signatures.

>>> import inspect
>>> no_sig = []
>>> has_sig = []
>>> for name, val in __builtins__.__dict__.items():
...     try:
...         tmp = inspect.signature(val)
...     except TypeError:
...         pass # not callable
...     except ValueError:
...         no_sig.append(name)
...     else:
...         has_sig.append(name)
...
>>>
>>> len(no_sig)
101
>>> len(has_sig)
40

Look at only "functions":

>>> [name for name in no_sig if type(getattr(__builtins__, name)) ==
type(print)]
['__build_class__', '__import__', 'dir', 'getattr', 'iter', 'max',
'min', 'next', 'print', 'round', 'vars']
>>> len([name for name in no_sig if type(getattr(__builtins__, name)) ==
type(print)])
11
>>> [name for name in has_sig if type(getattr(__builtins__, name)) ==
type(print)]
['abs', 'all', 'any', 'ascii', 'bin', 'callable', 'chr', 'compile',
'delattr', 'divmod', 'eval', 'exec', 'format', 'globals', 'hasattr',
'hash', 'hex', 'id', 'input', 'isinstance', 'issubclass', 'len',
'locals', 'oct', 'ord', 'pow', 'repr', 'setattr', 'sorted', 'sum', 'open']
>>> len([name for name in has_sig if type(getattr(__builtins__, name))
== type(print)])
31

Take out the exceptions and warnings:

>>> [name for name in no_sig if name[0] == name[0].lower()]
['__build_class__', '__import__', 'dir', 'getattr', 'iter', 'max',
'min', 'next', 'print', 'round', 'vars', 'bool', 'bytearray', 'bytes',
'classmethod', 'complex', 'dict', 'enumerate', 'filter', 'float',
'frozenset', 'property', 'int', 'list', 'map', 'range', 'reversed',
'set', 'slice', 'staticmethod', 'str', 'super', 'tuple', 'type', 'zip']
>>> len([name for name in no_sig if name[0] == name[0].lower()])
35
>>> [name for name in has_sig if name[0] == name[0].lower()]
['__loader__', 'abs', 'all', 'any', 'ascii', 'bin', 'callable', 'chr',
'compile', 'delattr', 'divmod', 'eval', 'exec', 'format', 'globals',
'hasattr', 'hash', 'hex', 'id', 'input', 'isinstance', 'issubclass',
'len', 'locals', 'oct', 'ord', 'pow', 'repr', 'setattr', 'sorted',
'sum', 'memoryview', 'object', 'open', 'quit', 'exit', 'copyright',
'credits', 'license', 'help']
>>> len([name for name in has_sig if name[0] == name[0].lower()])
40
>>>


-- 
Thomas Jollans



More information about the Python-list mailing list