Comparing file objects: what is compared? 
Author Message
 Comparing file objects: what is compared?

Hello,

Quote:
>>> 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?

regards,
Gerrit.

--
Homepage: http://www.*-*-*.com/ ~gerrit
-----BEGIN GEEK CODE BLOCK----- http://www.*-*-*.com/
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O
!M !V PS+ PE? Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK----- moc.edockeeg.www//:ptth



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?

Quote:

> 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?

File objects have no specialized "cmp" method, so get the default

cmp(fp1,fp2) == cmp(id(fp1),id(fp2))

one.

HTH,
Michael



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?

Quote:

> 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
around.
--

INTERNET: Learn what you know.
Share what you don't.



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?

Quote:
Gerrit Holl writes:

 > >>> 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?

  The ID is used for when nothing else is meaningful (and clear).
It's arbitrary & consistent.  ;)

  -Fred

--
Fred L. Drake, Jr.        <fdrake at acm.org>
Corporation for National Research Initiatives



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?
Fred L. Drake, Jr. wrote on 950270973:

Quote:

> Gerrit Holl writes:
>  > >>> 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?

>   The ID is used for when nothing else is meaningful (and clear).
> It's arbitrary & consistent.  ;)

This does not seem to be documented anywhere! Am I missing it or am I right?
I think it should be documented, something like:
"""
If objects being larger or smaller seems meaningless, like with file objects,
the result is pretty arbitrary: the internal id (as returned by the id()
builtin function) is used.
"""

Or as a FAQ. Or a an IAQ, since it's not asked Frequently but Infrequently
instead :)

regards,
Gerrit.

--
Homepage: http://www.nl.linux.org/~gerrit
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O
!M !V PS+ PE? Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK----- moc.edockeeg.www//:ptth



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?

Quote:
Gerrit Holl writes:

 > This does not seem to be documented anywhere! Am I missing it or am I right?
 > I think it should be documented, something like:

  I think it's documented as being arbitrary but consistent, which is
sufficient.  It's not important that the ID is used, just the current
implementation.

  -Fred

--
Fred L. Drake, Jr.        <fdrake at acm.org>
Corporation for National Research Initiatives



Tue, 30 Jul 2002 03:00:00 GMT  
 Comparing file objects: what is compared?

Quote:

> >>> 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?

http://www.python.org/doc/current/ref/comparisons.html
tells you how comparisions work.  this case falls in the
"other types" category:

    "Most other types compare unequal unless they are
    the same object; the choice whether one object is
    considered smaller or larger than another one is made
    arbitrarily but consistently within one execution of a
    program."

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->



Tue, 30 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Comparing 2 files to create a third file

2. comparing an input file with an output file

3. Beginners Q: Comparing objects in ST/80 R4

4. Object sorting, collection comparing

5. comparing object-oriented platforms

6. Compare object-oriented and function-oriented ....

7. Comparing objects

8. Q: Comparing objects

9. Functional and Object Oriented Languages compared

10. compare object references

11. Knowing if you can compare two objects

12. Comparing object identity for values

 

 
Powered by phpBB® Forum Software