What's the difference between "_io" and "io"?
I tried the code below. f
has a type _io.TextIOWrapper
, but I can't find any help on that type. While there is another similar type io.TextIOWrapper
.
>>> f=open("c:\setup.log","r")
>>> type(f)
<class '_io.TextIOWrapper'>
>>> help(_io.TextIOWrapper)
Traceback (most recent call last):
File "<pyshell#204>", line 1, in <module>
help(_io.TextIOWrapper)
NameError: name '_io' is not defined
>>> help(io.TextIOWrapper)
Help on class TextIOWrapper in module io:
My questions:
-
If the name is
_io
not defined, how can I use it? -
What is the difference between modules
_io
andio
?
source to share
The module _io
provides C code that the module io
uses internally. The source for this can be found here . You can import either io
or _io
separately:
>>> import _io
>>> import io
>>> _io
<module 'io' (built-in)> # The fact that this says io instead of _io is a bug (Issue 18602)
>>> io
<module 'io' from '/usr/lib/python3.4/io.py'>
>>> _io.TextIOWrapper
<type '_io.TextIOWrapper'>
This pattern (the C code for modulename
that presented in _modulename
) is actually used for multiple modules - multiprocessing
/ _multiprocessing
, csv
/ _csv
, etc. Basically all the cases where a module has a component written in C.
source to share
_io
is part of the C module implementation io
, io
part of python.
From PEP8 ;
If an extension module written in C or C ++ has an accompanying Python module that provides a higher level (for example, more object-oriented) interface, the C / C ++ module has a leading underscore (for example, _socket).
source to share