Home > Software design >  Where min, max and sum are defined in python?
Where min, max and sum are defined in python?

Time:04-26

I'm wondering where sum(), min(), and max() are defined in python? for example performing this operation:

a = [1,2,3,4]
min(a)

if I looking for into methods of a list min is not there:

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

CodePudding user response:

They are built-in functions:

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.

Built-in Functions

...

M
map()
max()
memoryview()
min()

...

If they were methods on list, you would call them as a.min() and a.max() instead.

The reason that these functions are global, and not methods, is that they are more universal: they work on any iterable. For example, you can also call min(x for x in a if x%2 == 0), where you're passing it a generator instead.

CodePudding user response:

The sum function is not a method of the list object, like you show there, but a global function.
It is built into python. See here: https://docs.python.org/3/library/functions.html?msclkid=c061f6fbc53b11ec9ef2424170aeda56#sum

  • Related