[Tutor] Test for type(object) == ???
eryk sun
eryksun at gmail.com
Sat Feb 11 00:49:02 EST 2017
On Sat, Feb 11, 2017 at 3:22 AM, boB Stepp <robertvstepp at gmail.com> wrote:
>
> py3: help(repr)
> Help on built-in function repr in module builtins:
>
> repr(obj, /)
> Return the canonical string representation of the object.
>
> For many object types, including most builtins, eval(repr(obj)) == obj.
>
> Question: What does the forward slash represent in this context?
It's from the function's __text_signature__.
>>> repr.__text_signature__
'($module, obj, /)'
It means "obj" is a positional-only argument that cannot be passed as a keyword.
>>> s = inspect.signature(repr)
>>> s.parameters['obj'].kind
<_ParameterKind.POSITIONAL_ONLY: 0>
CPython has a lot of built-in functions that don't allow keyword arguments.
>>> repr(obj=42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: repr() takes no keyword arguments
Look at open() for comparison.
>>> open.__text_signature__
"($module, /, file, mode='r', buffering=-1, encoding=None,\n
errors=None, newline=None, closefd=True, opener=None)"
All of its parameters can be passed as keyword arguments.
>>> s = inspect.signature(open)
>>> s.parameters['file'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>
>>> s.parameters['mode'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>
For example:
>>> open(file='spam', mode='w')
<_io.TextIOWrapper name='spam' mode='w' encoding='UTF-8'>
More information about the Tutor
mailing list