[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

min/max: "stable" invariant?

Giovanni Bajo

3/11/2008 11:09:00 PM

Hello,

assuming that a sequence contains multiple elements with minimum/maximum
value, do min/max guarantee to return the *first* element in the sequence
among those?

Example:

>>> class A:
.... def __init__(self, x): self.x = x
....
>>> L = [A(0), A(1), A(2), A(0)]
>>> min(L, key=lambda a:a.x) is L[0]
True
>>> min(L, key=lambda a:a.x) is L[3]
False

Is this guaranteed by the Python language formal specification?
Is this guaranteed to always by true in CPython? (I guess so)

I can't find any mention in the documentation.
--
Giovanni Bajo
1 Answer

Terry Reedy

3/11/2008 11:34:00 PM

0


"Giovanni Bajo" <rasky@develer.com> wrote in message
news:jiEBj.16245$q53.9583@tornado.fastwebnet.it...
| Hello,
|
| assuming that a sequence contains multiple elements with minimum/maximum
| value, do min/max guarantee to return the *first* element in the sequence
| among those?
|
| Example:
|
| >>> class A:
| ... def __init__(self, x): self.x = x
| ...
| >>> L = [A(0), A(1), A(2), A(0)]
| >>> min(L, key=lambda a:a.x) is L[0]
| True
| >>> min(L, key=lambda a:a.x) is L[3]
| False
|
| Is this guaranteed by the Python language formal specification?
| Is this guaranteed to always by true in CPython? (I guess so)
|
| I can't find any mention in the documentation.

If there is no mention in the documentation (Lib Ref, Ch2 builtin
functions), then there is no guarantee.