Prev: What's the largest python/django powered website in the world?
Next: how to generate a csr in python?
From: Payal on 31 May 2010 06:19 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? 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? Thanks a lot in advance. With warm regards, -Payal --
From: Colin J. Williams on 31 May 2010 06:48 On 31-May-10 06:19 AM, 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? > > 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? > > Thanks a lot in advance. > With warm regards, > -Payal Here is an extract from the docs for Python 2.6.5. I hope that this clarifies things. Colin W. 2.3.2. Reserved classes of identifiers Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: _* Not imported by from module import *. The special identifier _ is used in the interactive interpreter to store the result of the last evaluation; it is stored in the __builtin__ module. When not in interactive mode, _ has no special meaning and is not defined. See section The import statement. Note The name _ is often used in conjunction with internationalization; refer to the documentation for the gettext module for more information on this convention. __*__ System-defined names. These names are defined by the interpreter and its implementation (including the standard library); applications should not expect to define additional names using this convention. The set of names of this class defined by Python may be extended in future versions. See section Special method names. __* Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between �private� attributes of base and derived classes. See section Identifiers (Names).
From: Chris Rebert on 31 May 2010 06:47 On Mon, May 31, 2010 at 3:41 AM, Xavier Ho <contact(a)xavierho.com> wrote: > On 31 May 2010 20:19, Payal <payal-python(a)scriptkitchen.com> wrote: <snip> >> 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. To be a bit more specific, double-underscore methods are called internally by Python to implement various operators. For example, `a + b` is usually equivalent to `a.__add__(b)`, and `len(a)` calls `a.__len__()` internally. For more info, see http://docs.python.org/reference/datamodel.html#special-method-names Cheers, Chris -- http://blog.rebertia.com
From: Payal on 1 Jun 2010 02:43 On Mon, May 31, 2010 at 08:41:54PM +1000, Xavier Ho wrote: > Welcome (back) to the Python-List! Thanks a lot to all who replied. Special thanks to Xavier Ho for sample examples on iterators. That cleared the doubt. With warm regards, -Payal --
From: Gabriel Genellina on 5 Jun 2010 05:38 On 31 mayo, 07:19, Payal <payal-pyt...(a)scriptkitchen.com> wrote: > 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? You may want to install and use "see", a human-friendly replacement of dir() So instead of this mess: py> dir(pencil_case) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', ' __delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__get item__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', ' __init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__ ', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'a ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse' , 'sort'] you get this instead: py> see(pencil_case) [] in + += * *= < <= == != > >= hash() help() iter() len() repr() reversed() str() .append() .count() .extend() .index() .insert() .pop() .remove() .reverse() .sort() For us mere mortals, it's a lot more readable. "see" is available at http://github.com/inky/see -- Gabriel Genellina
|
Next
|
Last
Pages: 1 2 Prev: What's the largest python/django powered website in the world? Next: how to generate a csr in python? |