dict and __cmp__() question

A

Alex

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

Fredrik Lundh

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

I tried

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top