Tuesday, 2 August 2016

Why isn't there any special method for __max__ in python?

As the title asks. Python has a lot of special methods, __add__, __len__, __contains__ et c. Why is there no __max__ method that is called when doing max? Example code:



class A:
def __max__():

return 5

a = A()
max(a)


It seems like range() and other constructs could benefit from this. Am I missing some other effective way to do max?¨



Addendum 1:
As a trivial example, max(range(1000000000)) takes a long time to run.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...