reading help() - newbie question

Discussion in 'Python' started by Payal, May 31, 2010.

  1. Payal

    Payal Guest

    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
    --
     
    Payal, May 31, 2010
    #1
    1. Advertising

  2. On 31 mayo, 07:19, Payal <> 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
     
    Gabriel Genellina, Jun 5, 2010
    #2
    1. Advertising

  3. Payal

    Lie Ryan Guest

    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.
     
    Lie Ryan, Jun 5, 2010
    #3
  4. Payal

    Lie Ryan Guest

    On 06/05/10 21:24, Lie Ryan wrote:
    > 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.


    Ok, I just read what I wrote again and I noticed that the example isn't
    complete enough to illustrate what I'm talking about, so:

    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

    a = Comparable(10) # a.value = 10
    b = Comparable(20) # b.value = 20

    # the < operator calls __lt__ special method and this
    # prints False, because a.value > other.value is False
    print a < b

    # prints "Value: 10" since 'print' statement calls str() builtin
    # function which calls __str__ to turn objects into a string
    print a
     
    Lie Ryan, Jun 5, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markis Landis Gardner
    Replies:
    6
    Views:
    4,467
    Andrew Thompson
    Dec 25, 2003
  2. Ed
    Replies:
    4
    Views:
    655
  3. john
    Replies:
    11
    Views:
    739
    apochd
    Feb 10, 2011
  4. Jatinder Singh
    Replies:
    4
    Views:
    439
    Steve Holden
    Jun 1, 2005
  5. Jerry C.
    Replies:
    8
    Views:
    256
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page