Comparing file objects: what is compared?

Moshe Zadka moshez at
Fri Feb 11 18:39:46 CET 2000

On Fri, 11 Feb 2000, Gerrit Holl wrote:

> Hello,
> >>> fp1=open('/tmp/file_one', 'w')
> >>> fp2=open('/tmp/file_two', 'w')
> >>> cmp(fp1, fp2)
> -1
> >>> cmp(fp2, fp1)
> 1
> What is compared? The date of creation? The filename? The contents?
> I've searched the FAQ, the documentation, the sig archives and the
> newsgroup, but I'm not able to find the answer. What is compared when
> comparing file objects?

I don't know, don't care, and it's implementation dependant. The
important thing is that

1. Any two objects can be compared
2. The ordering is stable

(that is, if neither fp1 nor fp2 changes, fp1 will stay larger then fp2)

3. The ordering has the natural properties expected from an ordering.
(I.e., if a<b and b<c then a<c etc.)

The reason to even *have* comparison between file objects is so that
you could (for example) implement arbitarily-valued binary-trees 
(or red-black trees, if that's your preversion).

Note that this too might change: rich comparisons will turn all that
Moshe Zadka <mzadka at>. 
INTERNET: Learn what you know.
Share what you don't.

More information about the Python-list mailing list