Skip to content

max() works with greater-than or less-than #6575

Description

@JukkaL

Currently max() expects __gt__, but this example indicates that it also works if only __lt__ is present:

class Lt:
    def __init__(self, x):
        self.x = x

    def __repr__(self):
        return f"Lt({self.x})"

    def __lt__(self, other):
        return self.x < other.x

class Gt:
    def __init__(self, x):
        self.x = x

    def __repr__(self):
        return f"Gt({self.x})"

    def __gt__(self, other):
        return self.x > other.x

print(max(Lt(2), Lt(5)))  # Lt(5)
print(max(Lt(5), Lt(2)))  # Lt(5)
print(max(Gt(2), Gt(5)))  # Gt(5)
print(max(Gt(5), Gt(2)))  # Gt(5)

I think that we should accept either method.

The signature of max() was changed to expect __gt__ in #6342.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions