min/max: "stable" invariant?

G

Giovanni Bajo

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:
.... 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.
 
T

Terry Reedy

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top