Which python module contains file object methods?
While it is simple to search by using help
for most methods that have a clear help(module.method)
arrangement, for example help(list.extend)
, I cannot work out how to look up the method .readline()
in python's inbuilt help function.
Which module does .readline
belong to? How would I search in help
for .readline
and related methods?
Furthermore is there any way I can use the interpreter to find out which module a method belongs to in future?
python python-3.x
add a comment |
While it is simple to search by using help
for most methods that have a clear help(module.method)
arrangement, for example help(list.extend)
, I cannot work out how to look up the method .readline()
in python's inbuilt help function.
Which module does .readline
belong to? How would I search in help
for .readline
and related methods?
Furthermore is there any way I can use the interpreter to find out which module a method belongs to in future?
python python-3.x
1
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call.readline()
on?
– Chris
Nov 14 '18 at 2:56
3
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
1
As mentioned by @wim, thetype
of a regular call toopen('myfile.txt')
is an instance of that class
– Brad Solomon
Nov 14 '18 at 3:00
1
Side-note:list.extend
is not an example ofmodule.method
.list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).
– ShadowRanger
Nov 14 '18 at 3:07
add a comment |
While it is simple to search by using help
for most methods that have a clear help(module.method)
arrangement, for example help(list.extend)
, I cannot work out how to look up the method .readline()
in python's inbuilt help function.
Which module does .readline
belong to? How would I search in help
for .readline
and related methods?
Furthermore is there any way I can use the interpreter to find out which module a method belongs to in future?
python python-3.x
While it is simple to search by using help
for most methods that have a clear help(module.method)
arrangement, for example help(list.extend)
, I cannot work out how to look up the method .readline()
in python's inbuilt help function.
Which module does .readline
belong to? How would I search in help
for .readline
and related methods?
Furthermore is there any way I can use the interpreter to find out which module a method belongs to in future?
python python-3.x
python python-3.x
asked Nov 14 '18 at 2:54
The Blurst Of TimesThe Blurst Of Times
212
212
1
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call.readline()
on?
– Chris
Nov 14 '18 at 2:56
3
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
1
As mentioned by @wim, thetype
of a regular call toopen('myfile.txt')
is an instance of that class
– Brad Solomon
Nov 14 '18 at 3:00
1
Side-note:list.extend
is not an example ofmodule.method
.list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).
– ShadowRanger
Nov 14 '18 at 3:07
add a comment |
1
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call.readline()
on?
– Chris
Nov 14 '18 at 2:56
3
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
1
As mentioned by @wim, thetype
of a regular call toopen('myfile.txt')
is an instance of that class
– Brad Solomon
Nov 14 '18 at 3:00
1
Side-note:list.extend
is not an example ofmodule.method
.list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).
– ShadowRanger
Nov 14 '18 at 3:07
1
1
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call
.readline()
on?– Chris
Nov 14 '18 at 2:56
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call
.readline()
on?– Chris
Nov 14 '18 at 2:56
3
3
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
1
1
As mentioned by @wim, the
type
of a regular call to open('myfile.txt')
is an instance of that class– Brad Solomon
Nov 14 '18 at 3:00
As mentioned by @wim, the
type
of a regular call to open('myfile.txt')
is an instance of that class– Brad Solomon
Nov 14 '18 at 3:00
1
1
Side-note:
list.extend
is not an example of module.method
. list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).– ShadowRanger
Nov 14 '18 at 3:07
Side-note:
list.extend
is not an example of module.method
. list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).– ShadowRanger
Nov 14 '18 at 3:07
add a comment |
2 Answers
2
active
oldest
votes
Don't try to find the module. Make an instance of the class you want, then call help on the method of that instance, and it will find the correct help info for you. Example:
>>> f = open('pathtosomefile')
>>> help(f.readline)
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
In my case (Python 3.7.1), it's defined on the type _io.TextIOWrapper
(exposed publicly as io.TextIOWrapper
, but help
doesn't know that), but memorizing that sort of thing isn't very helpful. Knowing how to figure it out by introspecting the specific thing you care about is much more broadly applicable. In this particular case, it's extra important not to try guessing, because the open
function can return a few different classes, each with different methods, depending on the arguments provided, including io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
, and io.FileIO
, each with their own version of the readline
method (though they all share a similar interface for consistency's sake).
add a comment |
From the text of help(open)
:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom.
See also the section of python's io
module documentation on the class hierarchy.
So you're looking at TextIOWrapper
, BufferedReader
, BufferedWriter
, or BufferedRandom
. These all have their own sets of class hierarchies, but suffice it to say that they share the IOBase
superclass at some point - that's where the functions readline()
and readlines()
are declared. Of course, each subclass implements these functions differently for its particular mode - if you do
help(_io.TextIOWrapper.readline)
you should get the documentation you're looking for.
In particular, you're having trouble accessing the documentation for whichever version of readline
you need, because you can't be bothered to figure out which class it is. You can actually call help
on an object as well. If you're working with a particular file object, then you can spin up a terminal, instantiate it, and then just pass it to help()
and it'll show you whatever interface is closest to the surface. Example:
x = open('some_file.txt', 'r')
help(x.readline)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292518%2fwhich-python-module-contains-file-object-methods%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't try to find the module. Make an instance of the class you want, then call help on the method of that instance, and it will find the correct help info for you. Example:
>>> f = open('pathtosomefile')
>>> help(f.readline)
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
In my case (Python 3.7.1), it's defined on the type _io.TextIOWrapper
(exposed publicly as io.TextIOWrapper
, but help
doesn't know that), but memorizing that sort of thing isn't very helpful. Knowing how to figure it out by introspecting the specific thing you care about is much more broadly applicable. In this particular case, it's extra important not to try guessing, because the open
function can return a few different classes, each with different methods, depending on the arguments provided, including io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
, and io.FileIO
, each with their own version of the readline
method (though they all share a similar interface for consistency's sake).
add a comment |
Don't try to find the module. Make an instance of the class you want, then call help on the method of that instance, and it will find the correct help info for you. Example:
>>> f = open('pathtosomefile')
>>> help(f.readline)
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
In my case (Python 3.7.1), it's defined on the type _io.TextIOWrapper
(exposed publicly as io.TextIOWrapper
, but help
doesn't know that), but memorizing that sort of thing isn't very helpful. Knowing how to figure it out by introspecting the specific thing you care about is much more broadly applicable. In this particular case, it's extra important not to try guessing, because the open
function can return a few different classes, each with different methods, depending on the arguments provided, including io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
, and io.FileIO
, each with their own version of the readline
method (though they all share a similar interface for consistency's sake).
add a comment |
Don't try to find the module. Make an instance of the class you want, then call help on the method of that instance, and it will find the correct help info for you. Example:
>>> f = open('pathtosomefile')
>>> help(f.readline)
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
In my case (Python 3.7.1), it's defined on the type _io.TextIOWrapper
(exposed publicly as io.TextIOWrapper
, but help
doesn't know that), but memorizing that sort of thing isn't very helpful. Knowing how to figure it out by introspecting the specific thing you care about is much more broadly applicable. In this particular case, it's extra important not to try guessing, because the open
function can return a few different classes, each with different methods, depending on the arguments provided, including io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
, and io.FileIO
, each with their own version of the readline
method (though they all share a similar interface for consistency's sake).
Don't try to find the module. Make an instance of the class you want, then call help on the method of that instance, and it will find the correct help info for you. Example:
>>> f = open('pathtosomefile')
>>> help(f.readline)
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
In my case (Python 3.7.1), it's defined on the type _io.TextIOWrapper
(exposed publicly as io.TextIOWrapper
, but help
doesn't know that), but memorizing that sort of thing isn't very helpful. Knowing how to figure it out by introspecting the specific thing you care about is much more broadly applicable. In this particular case, it's extra important not to try guessing, because the open
function can return a few different classes, each with different methods, depending on the arguments provided, including io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
, and io.FileIO
, each with their own version of the readline
method (though they all share a similar interface for consistency's sake).
answered Nov 14 '18 at 3:14
ShadowRangerShadowRanger
61.2k55796
61.2k55796
add a comment |
add a comment |
From the text of help(open)
:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom.
See also the section of python's io
module documentation on the class hierarchy.
So you're looking at TextIOWrapper
, BufferedReader
, BufferedWriter
, or BufferedRandom
. These all have their own sets of class hierarchies, but suffice it to say that they share the IOBase
superclass at some point - that's where the functions readline()
and readlines()
are declared. Of course, each subclass implements these functions differently for its particular mode - if you do
help(_io.TextIOWrapper.readline)
you should get the documentation you're looking for.
In particular, you're having trouble accessing the documentation for whichever version of readline
you need, because you can't be bothered to figure out which class it is. You can actually call help
on an object as well. If you're working with a particular file object, then you can spin up a terminal, instantiate it, and then just pass it to help()
and it'll show you whatever interface is closest to the surface. Example:
x = open('some_file.txt', 'r')
help(x.readline)
add a comment |
From the text of help(open)
:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom.
See also the section of python's io
module documentation on the class hierarchy.
So you're looking at TextIOWrapper
, BufferedReader
, BufferedWriter
, or BufferedRandom
. These all have their own sets of class hierarchies, but suffice it to say that they share the IOBase
superclass at some point - that's where the functions readline()
and readlines()
are declared. Of course, each subclass implements these functions differently for its particular mode - if you do
help(_io.TextIOWrapper.readline)
you should get the documentation you're looking for.
In particular, you're having trouble accessing the documentation for whichever version of readline
you need, because you can't be bothered to figure out which class it is. You can actually call help
on an object as well. If you're working with a particular file object, then you can spin up a terminal, instantiate it, and then just pass it to help()
and it'll show you whatever interface is closest to the surface. Example:
x = open('some_file.txt', 'r')
help(x.readline)
add a comment |
From the text of help(open)
:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom.
See also the section of python's io
module documentation on the class hierarchy.
So you're looking at TextIOWrapper
, BufferedReader
, BufferedWriter
, or BufferedRandom
. These all have their own sets of class hierarchies, but suffice it to say that they share the IOBase
superclass at some point - that's where the functions readline()
and readlines()
are declared. Of course, each subclass implements these functions differently for its particular mode - if you do
help(_io.TextIOWrapper.readline)
you should get the documentation you're looking for.
In particular, you're having trouble accessing the documentation for whichever version of readline
you need, because you can't be bothered to figure out which class it is. You can actually call help
on an object as well. If you're working with a particular file object, then you can spin up a terminal, instantiate it, and then just pass it to help()
and it'll show you whatever interface is closest to the surface. Example:
x = open('some_file.txt', 'r')
help(x.readline)
From the text of help(open)
:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom.
See also the section of python's io
module documentation on the class hierarchy.
So you're looking at TextIOWrapper
, BufferedReader
, BufferedWriter
, or BufferedRandom
. These all have their own sets of class hierarchies, but suffice it to say that they share the IOBase
superclass at some point - that's where the functions readline()
and readlines()
are declared. Of course, each subclass implements these functions differently for its particular mode - if you do
help(_io.TextIOWrapper.readline)
you should get the documentation you're looking for.
In particular, you're having trouble accessing the documentation for whichever version of readline
you need, because you can't be bothered to figure out which class it is. You can actually call help
on an object as well. If you're working with a particular file object, then you can spin up a terminal, instantiate it, and then just pass it to help()
and it'll show you whatever interface is closest to the surface. Example:
x = open('some_file.txt', 'r')
help(x.readline)
answered Nov 14 '18 at 3:20
Green Cloak GuyGreen Cloak Guy
2,9191720
2,9191720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292518%2fwhich-python-module-contains-file-object-methods%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That function can exist in any number of modules (or classes). That's the whole point of modules: they provide a namespace in which things can exist. The one you want documentation on depends on your own specific needs. What are you trying to call
.readline()
on?– Chris
Nov 14 '18 at 2:56
3
Probably you want io.TextIOWrapper.readline
– wim
Nov 14 '18 at 2:57
1
As mentioned by @wim, the
type
of a regular call toopen('myfile.txt')
is an instance of that class– Brad Solomon
Nov 14 '18 at 3:00
1
Side-note:
list.extend
is not an example ofmodule.method
.list
is a built-in type, not a module. Also, technically, almost everything found on a module directly is a function, not a method ("methods" are functions defined on classes and therefore accessible through class instances; top-level functions on a module aren't usually part of a class).– ShadowRanger
Nov 14 '18 at 3:07