473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

dict and __cmp__() question

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
2 1957
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jan-Erik Meyer-Lütgens | last post by:
In the Python Language Reference, I found the following statements about using objects as dictionary keys: 1. "__hash__() should return a 32-bit integer." 2. "The only required property is...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
3
by: Victor Safronovich | last post by:
please comment this Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 >>> class A: def __cmp__(self, other): print '%s|%s' %(`self`, `other`) return cmp(self, other) >>> a = A() >>>...
8
by: bearophileHUGS | last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less...
9
by: Neil Benn | last post by:
Hello, I can't find the docs for __eq__ on a dict and I can't find a description on what the eq does (strangely it does implement > and < but I have no idea what that does). Does anyone know...
39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
8
by: insyte | last post by:
I have a class that has, as an attribute, an instance of datetime.datetime(). I would like to be able to compare my class directly to instances of datetime.datetime in addition to other instances...
6
by: Adam Olsen | last post by:
It seems to be a commonly held belief that basic dict operations (get, set, del) are atomic. However, since I know searching the hash table is a multistep process, I thought I'd check it out for...
17
by: Johannes Bauer | last post by:
Hello group, I'm porting some code of mine to Python 3. One class has the __cmp__ operator overloaded, but comparison doesn't seem to work anymore with that: Traceback (most recent call last):...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.