dict and __cmp__() question

Discussion in 'Python' started by Alex, Sep 7, 2005.

  1. Alex

    Alex Guest

    Entering

    >>> help(dict)

    Help on class dict in module __builtin__:

    class dict(object)
    | dict() -> new empty dictionary.
    | dict(mapping) -> new dictionary initialized from a mapping object's
    | (key, value) pairs.
    | dict(seq) -> new dictionary initialized as if via:
    | d = {}
    | for k, v in seq:
    | d[k] = v
    | dict(**kwargs) -> new dictionary initialized with the name=value
    pairs
    | in the keyword argument list. For example: dict(one=1, two=2)
    |
    | Methods defined here:
    |
    | __cmp__(...)
    | x.__cmp__(y) <==> cmp(x,y)
    |
    | __contains__(...)
    | D.__contains__(k) -> True if D has a key k, else False

    snip

    | update(...)
    | D.update(E, **F) -> None. Update D from E and F: for k in E:
    D[k] = E[k]
    | (if E has keys else: for (k, v) in E: D[k] = v) then: for k in
    F: D[k] = F[k]
    |
    | values(...)
    | D.values() -> list of D's values

    Now I understand methods like update(...) and values(...), for instance

    >>> D={'a':1, 'b':2}
    >>> D.values()

    [1, 2]
    >>>


    But what are those with double underscore? For instance __cmp__(...)?

    I tried
    >>> D.cmp('a','b')


    Traceback (most recent call last):
    File "<pyshell#7>", line 1, in -toplevel-
    D.cmp('a','b')
    AttributeError: 'dict' object has no attribute 'cmp'
    >>>


    Alex
     
    Alex, Sep 7, 2005
    #1
    1. Advertising

  2. "Alex" <> wrote:

    > But what are those with double underscore? For instance __cmp__(...)?
    >
    > I tried
    >>>> D.cmp('a','b')


    make that

    cmp('a', 'b')

    methods that start and end with "__" are implementation hooks:

    http://docs.python.org/ref/specialnames.html

    __cmp__ is used by cmp(a, b) and other operations that need to compare
    things (unless "rich comparision" hooks are defined; see

    http://docs.python.org/ref/customization.html

    )

    other common hooks are __init__ (called after construction), __len__ (called
    to determine the length of a sequence), __getitem__ (called to fetch an item
    from a container), and a few others. see the documentation for details.

    </F>
     
    Fredrik Lundh, Sep 7, 2005
    #2
    1. Advertising

  3. Alex

    Bryan Olson Guest

    Bryan Olson, Sep 7, 2005
    #3
    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. Jim Fulton
    Replies:
    0
    Views:
    669
    Jim Fulton
    Jun 29, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    594
    Tim Peters
    Jun 29, 2003
  3. Lee Harr

    custom sorting and __cmp__

    Lee Harr, Nov 30, 2003, in forum: Python
    Replies:
    3
    Views:
    467
  4. Thomas Nelson

    Trouble with max() and __cmp__()

    Thomas Nelson, Jan 28, 2007, in forum: Python
    Replies:
    4
    Views:
    275
    Gabriel Genellina
    Jan 29, 2007
  5. Drew
    Replies:
    19
    Views:
    1,352
    Duncan Booth
    Mar 15, 2007
Loading...

Share This Page