[Python] Max function behavior [duplicate]

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm reading the Python tutorial. Does anyone know how this code works if max() expects a list?

    >>> class student:
    ... def __init__(self, gpa, name):
    ... self.gpa = gpa
    ... self.name = name
    ...
    >>> graduates = [student(1.0, "nick"), student(2.0, "geoff")]
    >>> m = max((s.gpa, s.name) for s in graduates)
    >>> print(m)
    (2.0, 'Geoff')


    I didn't know max could operate on a tuple. It looks like it's only comparing the first position of the tuple.

    Continue reading...

Compartilhe esta Página