On 05/31/10 20:19, Payal 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) > > When I type help(something) e.g. help(list), I see many methods like, > __methodname__(). Are these something special? How do I use them and why > put "__" around them?
Yes, the double-underscore are hooks to the various python protocols. They defines, among all, operator overloading, class construction and initialization, iterator protocol, descriptor protocol, type-casting, etc. A typical usage of these double-underscore is to create a class that overrides these functions, e.g.: class Comparable(object): def __init__(self, value): self.value = value def __lt__(self, other): return self.value > other.value def __gt__(self, other): return self.value < other.value def __str__(self): return "Value: " + self.value You should never create your own double-underscore method, just override/use the ones that Python provide. > 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 and how do I use these > methods because simly saying D.iteritems() does not work? > read about iterator protocol. Basically, the iterator protocol allows for-looping over a user-defined class (e.g. for emulating a collection). D.iteritems() returns an iterator object, which for-loop knows how to iterate over to generate the stream of (key, value) pairs. -- http://mail.python.org/mailman/listinfo/python-list