What variable type is returned from Open()?
Chris Angelico
rosuav at gmail.com
Thu Apr 16 14:37:13 EDT 2020
On Fri, Apr 17, 2020 at 2:13 AM Richard Damon <Richard at damon-family.org> wrote:
> I get the answer: <class '_io.TextIOWrapper'>
>
> So that is the name of the type that is returned, at least for that
> call. One key thing to note is that it begins with a _, so that type is
> actually an implementation detail, subject to change.
This is somewhat true; but when the *module* begins with an
underscore, it often means it's a C accelerator for a Python module.
You can often find the same thing exposed in a more visible way:
>>> import io
>>> io.TextIOWrapper
<class '_io.TextIOWrapper'>
And in that location, it is fully documented:
https://docs.python.org/3/library/io.html#io.TextIOWrapper
The fact that it comes from the accelerator *is* an internal
implementation detail, but the class itself is a public one.
That said, though: it is still very much incorrect to type hint in
this way. The correct type hint is a generic one:
https://docs.python.org/3/library/typing.html#typing.IO
And the correct way to encode this on a variable is to let type
inference figure it out. You'd use typing.IO or typing.TextIO to
annotate a function parameter, perhaps, but don't annotate your
variables at all.
ChrisA
More information about the Python-list
mailing list