Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find documentation about methods etc. for iterators

0 views
Skip to first unread message

tin...@isbd.co.uk

unread,
Apr 9, 2008, 12:05:21 PM4/9/08
to
I'm not sure if I have even phrased that right but anyway....

How does one find (in the standard Python documentation) information
about things like the iteritems() method and the enumerate() function.

They are mentioned in the tutorial as ways of getting more information
as you loop through an object but there seems to be no easy way to
find the definitive documentation of these sorts of methods and
functions. OK, if I know the name before I start I can probably find
what I want, but what if I want to know how to extract some
information from an object as I loop and don't know what I want is
called?

My particular quest that raised this was a way to get the line number
as I iterate through the lines of a file:-

f = open(fn, 'r')
lineNo = 0
for ln in f:
lineNo += 1

Is there a neater way of getting that line number as I go? If so how
am I meant to find out about it?

--
Chris Green

Arnaud Delobelle

unread,
Apr 9, 2008, 12:18:44 PM4/9/08
to

There is a neater way, and you mention it in your question:

f = open('myfile')

for lineno, line in enumerate(f):
# Do stuff

How to find out about it? I suppose you need to get familiar with
python iterators/iterables and the functions to manipulate them. How
to do that depends on your prior experience with such concepts.
Unfortunately I am not able to provide you with a good link :(

--
Arnaud

Terry Reedy

unread,
Apr 9, 2008, 6:37:46 PM4/9/08
to pytho...@python.org

<tin...@isbd.co.uk> wrote in message
news:47fce941$0$755$bed6...@news.gradwell.net...

| I'm not sure if I have even phrased that right but anyway....
|
| How does one find (in the standard Python documentation) information
| about things like the iteritems() method and the enumerate() function.

The Library Reference manual sections on builtin functions and dict
methods.

Or, help(enumerate) and help({}.iteritems)


tjr

tin...@isbd.co.uk

unread,
Apr 10, 2008, 4:24:24 AM4/10/08
to
.... but that doesn't address my problem really, how do I know that I
need to look for the words enumerate and/or iteritems? This is what
my original question was about.

There I was thinking that there has to be an easy way to get line
numbers as I read lines from a file but not knowing how to find out
how to do it:-

First question, what sort of 'thing' is the file object, I need to
know that if I'm to look up ways of using it.

Second question, even if I know what sort of thing a file object
is, how do I find methods applicable to it and/or functions
applicable to it?

The possibility that I might want either a method or a function adds
to the fun. In my original query it seemed odd that some objects have
the iteritems *method* whereas other objects have the enumerate
*function*.

It's a common problem in all sorts of computer fields, if you know the
name of what you want it's easy to find out details of how to use it
but if you don't know its name (or even if it exists) it's much more
difficult to find.

I've only been using Python for a few months and most of the time I
can find my way to what I need but this area of "what things can I do
with this object" still eludes me sometimes. What *I* need (I'm not
sure if this is a universal requirement though) is some consistent way
of firstly finding out what sort of an object something is (i.e. in
this case, what sort of object is a file) and then getting a list of
methods that I can apply to that object (O.K., this may need some
hierachy or other classification to keep it sane, but hopefully you
can see where I'm going).

--
Chris Green

Gabriel Genellina

unread,
Apr 10, 2008, 5:09:22 AM4/10/08
to pytho...@python.org
En Thu, 10 Apr 2008 05:24:24 -0300, <tin...@isbd.co.uk> escribió:

> Terry Reedy <tjr...@udel.edu> wrote:
>>
>> <tin...@isbd.co.uk> wrote in message
>> news:47fce941$0$755$bed6...@news.gradwell.net...
>> | I'm not sure if I have even phrased that right but anyway....
>> |
>> | How does one find (in the standard Python documentation) information
>> | about things like the iteritems() method and the enumerate() function.
>>
>> The Library Reference manual sections on builtin functions and dict
>> methods.
>>
>> Or, help(enumerate) and help({}.iteritems)
>>
> .... but that doesn't address my problem really, how do I know that I
> need to look for the words enumerate and/or iteritems? This is what
> my original question was about.

You'll have to read at least section 2 (built-in objects) and section 3
(built-in types) in the Library Reference: http://docs.python.org/lib/
That's the most important part to know.

> There I was thinking that there has to be an easy way to get line
> numbers as I read lines from a file but not knowing how to find out
> how to do it:-
>
> First question, what sort of 'thing' is the file object, I need to
> know that if I'm to look up ways of using it.
>
> Second question, even if I know what sort of thing a file object
> is, how do I find methods applicable to it and/or functions
> applicable to it?

You look for 'file object' in the index and find
http://docs.python.org/lib/bltin-file-objects.html

> The possibility that I might want either a method or a function adds
> to the fun. In my original query it seemed odd that some objects have
> the iteritems *method* whereas other objects have the enumerate
> *function*.

Other objects don't "have" the enumerate function, enumerate is a builtin
function that can be used with any sequence or iterable object. Your know
it because you have read section 2.1 in the Library Reference as I've told
you a few lines above :)

> It's a common problem in all sorts of computer fields, if you know the
> name of what you want it's easy to find out details of how to use it
> but if you don't know its name (or even if it exists) it's much more
> difficult to find.

Yes, certainly. Python comes with "batteries included" and there are many
of them. You don't have to read the whole Library Reference from begin to
end, but at least have a look at the titles so you know that certain thing
exists.

> I've only been using Python for a few months and most of the time I
> can find my way to what I need but this area of "what things can I do
> with this object" still eludes me sometimes. What *I* need (I'm not
> sure if this is a universal requirement though) is some consistent way
> of firstly finding out what sort of an object something is (i.e. in
> this case, what sort of object is a file) and then getting a list of
> methods that I can apply to that object (O.K., this may need some
> hierachy or other classification to keep it sane, but hopefully you
> can see where I'm going).

A file is... a file:

py> f = open("temp.txt","r")
py> type(f)
<type 'file'>

You can invoke the help system with any object:

py> help(f)
Help on file object:

class file(object)
| file(name[, mode[, buffering]]) -> file object
|
| Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
| writing or appending. The file will be created if it doesn't exist
| when opened for writing or appending; [blah...]
[... including all methods defined in the file class ...]

You can use help with a particular methods or function too:

py> help(f.write)
Help on built-in function write:

write(...)
write(str) -> None. Write string str to file.

Note that due to buffering, [blah...]

You can use dir(something) to see which attributes (including methods) an
object has:

py> dir(f)
['__class__', '__delattr__', '__doc__',
...
'close', 'closed', 'encoding', ... 'write', 'writelines', 'xreadlines']

Hope this helps.

--
Gabriel Genellina

Tim Chase

unread,
Apr 10, 2008, 5:54:29 AM4/10/08
to tin...@isbd.co.uk, pytho...@python.org
> First question, what sort of 'thing' is the file object, I need to
> know that if I'm to look up ways of using it.

you can always use

type(thing)

to find out what type it is.

> Second question, even if I know what sort of thing a file object
> is, how do I find methods applicable to it and/or functions
> applicable to it?

If you don't have a web-connection under your finger tips, using

dir(thing)
help(thing)
help(thing.method)

will tell you most of what you need to know. There's the
occasional gap, and it doesn't always work when the underlying
object is out in C-land, rather than a pure Python object that's
well-designed with doc-strings (personal complaint about
mod_python's failure to return dir() contents last I checked).
But for the most part I can just pull up a console debug-session
with Python, enter enough to get an instance of the object in
question, and then use the above commands.

This for me is Python's chief selling point: dir()....dir() and
help(). Python's two selling points are dir(), help(), and very
readable code. Python's *three* selling points are dir(),
help(), very readable code, and an almost fanatical devotion to
the BFDL. Amongst Python's selling points are such elements as
dir(), help()...I'll come in again. </python>

> It's a common problem in all sorts of computer fields, if you know the
> name of what you want it's easy to find out details of how to use it
> but if you don't know its name (or even if it exists) it's much more
> difficult to find.

All Monty Python-quoting aside, Python has solved this (and gives
3rd-party library developers the tools to solve as well) problem
of discoverability using dir() and help().

-tkc


cokof...@gmail.com

unread,
Apr 10, 2008, 8:07:15 AM4/10/08
to
>
> This for me is Python's chief selling point: dir()....dir() and
> help(). Python's two selling points are dir(), help(), and very
> readable code. Python's *three* selling points are dir(),
> help(), very readable code, and an almost fanatical devotion to
> the BFDL. Amongst Python's selling points are such elements as
> dir(), help()...I'll come in again. </python>
>

This brought a smile to my face :)

Terry Reedy

unread,
Apr 10, 2008, 12:13:26 PM4/10/08
to pytho...@python.org

<tin...@isbd.co.uk> wrote in message
news:47fdceb8$0$754$bed6...@news.gradwell.net...

| Terry Reedy <tjr...@udel.edu> wrote:
| >
| > <tin...@isbd.co.uk> wrote in message
| > news:47fce941$0$755$bed6...@news.gradwell.net...
| > | I'm not sure if I have even phrased that right but anyway....
| > |
| > | How does one find (in the standard Python documentation) information
| > | about things like the iteritems() method and the enumerate()
function.
| >
| > The Library Reference manual sections on builtin functions and dict
| > methods.
| >
| > Or, help(enumerate) and help({}.iteritems)
| >
| .... but that doesn't address my problem really, how do I know that I
| need to look for the words enumerate and/or iteritems? This is what
| my original question was about.

Do what Gabriel said: read chapters 2 and 3 of the Lib Manual. You will
not necessarily remember everything, but you will have an idea of what
functionalities exist and know to go look again. In a few months, read
them again.

As for the stdlib, at least scan through the table of contents so you have
a general idea of what there is. The documentation of modules (as well as
of builtins) is much improved from 10 years ago, when the only doc for some
was the code.


tjr

tin...@isbd.co.uk

unread,
Apr 10, 2008, 4:50:51 PM4/10/08
to
OK, thanks all for the replies. I know I will get more familiar with
what's available as I use Python more. I have the O'Reilly "Python in
a Nutshell" which I use for basic reference, with the helpful replies
in this thread I should be able to find my way.

--
Chris Green

0 new messages