dict and __cmp__() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    dict and __cmp__() question

    Entering
    [color=blue][color=green][color=darkred]
    >>> help(dict)[/color][/color][/color]
    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
    [color=blue][color=green][color=darkred]
    >>> D={'a':1, 'b':2}
    >>> D.values()[/color][/color][/color]
    [1, 2][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

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

    I tried[color=blue][color=green][color=darkred]
    >>> D.cmp('a','b')[/color][/color][/color]

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

    Alex

  • Fredrik Lundh

    #2
    Re: dict and __cmp__() question

    "Alex" <lidenalex@yaho o.se> wrote:
    [color=blue]
    > But what are those with double underscore? For instance __cmp__(...)?
    >
    > I tried[color=green][color=darkred]
    >>>> D.cmp('a','b')[/color][/color][/color]

    make that

    cmp('a', 'b')

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



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



    )

    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>



    Comment

    • Bryan Olson

      #3
      Re: dict and __cmp__() question

      Alex wrote:[color=blue]
      > But what are those with double underscore? For instance __cmp__(...)?[/color]

      Those are these:




      --
      --Bryan

      Comment

      Working...