By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

dict and __cmp__() question

P: n/a
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

Sep 7 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Alex" <li*******@yahoo.se> 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>

Sep 7 '05 #2

P: n/a
Alex wrote:
But what are those with double underscore? For instance __cmp__(...)?


Those are these:

http://docs.python.org/ref/specialnames.html
--
--Bryan
Sep 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.