reading help() - newbie question

Xavier Ho contact at xavierho.com
Mon May 31 06:41:54 EDT 2010


On 31 May 2010 20:19, Payal <payal-python at scriptkitchen.com> wrote:

> Hi,
> I am trying to learn Python (again) and have some basic doubts which I
> hope someone in the list can address. (English is not my first language and
> I
> have no CS background except I can write decent shell scripts)
>
>
Welcome (back) to the Python-List!


> When I type help(something) e.g. help(list), I see many methods like,
> __methodname__(). Are these something special?


They're very special. You can think of them as "Python internal functions",
and are called internally by other functions.


> How do I use them and why
> put "__" around them?
>

You call them as if they were any other function. 99% of the time though,
you don't need to call them, as there are better, cleaner ways.


>
> One more simple query. Many times I see something like this,
> |      D.iteritems() -> an iterator over the (key, value) items of D
> What is this iterator they are talking about <...>
>

See http://docs.python.org/library/stdtypes.html#iterator-types . Just a
peek, nothing big.


> and how do I use these
>

You can use iterators by ... iterating through the items! Like this:

>>> for i in [1, 3, 6, 10, 15]:
...     print i
...
1
3
6
10
15

and, in your specific example:

>>> x = {1: 2, 3: 4}
>>> for key, value in x.iteritems():
...     print key, "->", value
...
1 -> 2
3 -> 4


> methods because simly saying D.iteritems() does not work?<http://mail.python.org/mailman/listinfo/python-list>


It does work - it returns you an iterator you can use later:

>>> x_items = x.iteritems()
>>> for k, v in x_items:
...     print k + v
...
3
7

Have fun with Python!

Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100531/1f6a17de/attachment-0001.html>


More information about the Python-list mailing list