How to find documentation about methods etc. for iterators

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 10 05:09:22 EDT 2008


En Thu, 10 Apr 2008 05:24:24 -0300, <tinnews at isbd.co.uk> escribió:

> Terry Reedy <tjreedy at udel.edu> wrote:
>>
>> <tinnews at isbd.co.uk> wrote in message
>> news:47fce941$0$755$bed64819 at 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




More information about the Python-list mailing list